forked from wrenn/python-sdk
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4924237a23 | |||
| de72dfe9c8 | |||
| 2b10fde45b | |||
| 800a8566db | |||
| a42f0b2e71 | |||
| be573d07a3 | |||
| d46a715839 |
24
.github/workflows/release.yml
vendored
Normal file
24
.github/workflows/release.yml
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
name: Publish to PyPI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
pypi-publish:
|
||||
name: Upload release to PyPI
|
||||
runs-on: ubuntu-latest
|
||||
environment: pypi
|
||||
permissions:
|
||||
id-token: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: astral-sh/setup-uv@v6
|
||||
|
||||
- name: Build package
|
||||
run: uv build
|
||||
|
||||
- name: Publish package distributions to PyPI
|
||||
uses: pypa/gh-action-pypi-publish@release/v1
|
||||
8
.gitignore
vendored
8
.gitignore
vendored
@ -174,3 +174,11 @@ cython_debug/
|
||||
# PyPI configuration file
|
||||
.pypirc
|
||||
|
||||
CODE_EXECUTION.md
|
||||
|
||||
.opencode/
|
||||
# AI
|
||||
.code-review-graph/
|
||||
.claude
|
||||
.mcp.json
|
||||
AGENTS.md
|
||||
|
||||
25
.pre-commit-config.yaml
Normal file
25
.pre-commit-config.yaml
Normal file
@ -0,0 +1,25 @@
|
||||
repos:
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.15.10
|
||||
hooks:
|
||||
- id: ruff
|
||||
- id: ruff-format
|
||||
|
||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||
rev: v1.20.0
|
||||
hooks:
|
||||
- id: mypy
|
||||
additional_dependencies:
|
||||
- pydantic>=2.12.5
|
||||
- httpx>=0.28.1
|
||||
- httpx-ws>=0.9.0
|
||||
- email-validator>=2.3.0
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: unit-tests
|
||||
name: unit tests
|
||||
entry: uv run pytest -m "not integration" -x -q
|
||||
language: system
|
||||
pass_filenames: false
|
||||
always_run: true
|
||||
20
.woodpecker/code-runner.yml
Normal file
20
.woodpecker/code-runner.yml
Normal file
@ -0,0 +1,20 @@
|
||||
# E2E — code_runner. PR to dev/main when code_runner sources/tests change.
|
||||
when:
|
||||
- event: pull_request
|
||||
branch: [main, dev]
|
||||
path:
|
||||
include:
|
||||
- "src/wrenn/code_runner/**"
|
||||
- "tests/test_code_runner_*.py"
|
||||
- "pyproject.toml"
|
||||
- "uv.lock"
|
||||
|
||||
steps:
|
||||
test-code-runner:
|
||||
image: ghcr.io/astral-sh/uv:python3.13-bookworm
|
||||
environment:
|
||||
WRENN_API_KEY:
|
||||
from_secret: WRENN_API_KEY
|
||||
commands:
|
||||
- uv sync --dev
|
||||
- make test-code-runner
|
||||
25
.woodpecker/integration.yml
Normal file
25
.woodpecker/integration.yml
Normal file
@ -0,0 +1,25 @@
|
||||
# E2E — integration. PR to dev/main when non-code_runner src changes.
|
||||
# Path filter: include src/** but exclude src/wrenn/code_runner/** so the
|
||||
# dedicated code-runner pipeline owns that surface.
|
||||
when:
|
||||
- event: pull_request
|
||||
branch: [main, dev]
|
||||
path:
|
||||
include:
|
||||
- "src/**"
|
||||
- "tests/**"
|
||||
- "pyproject.toml"
|
||||
- "uv.lock"
|
||||
exclude:
|
||||
- "src/wrenn/code_runner/**"
|
||||
- "tests/test_code_runner_*.py"
|
||||
|
||||
steps:
|
||||
test-integration:
|
||||
image: ghcr.io/astral-sh/uv:python3.13-bookworm
|
||||
environment:
|
||||
WRENN_API_KEY:
|
||||
from_secret: WRENN_API_KEY
|
||||
commands:
|
||||
- uv sync --dev
|
||||
- make test-integration
|
||||
11
.woodpecker/unit.yml
Normal file
11
.woodpecker/unit.yml
Normal file
@ -0,0 +1,11 @@
|
||||
# Unit tests — every push and pull_request, all branches.
|
||||
when:
|
||||
- event: push
|
||||
- event: pull_request
|
||||
|
||||
steps:
|
||||
unit-tests:
|
||||
image: ghcr.io/astral-sh/uv:python3.13-bookworm
|
||||
commands:
|
||||
- uv sync --dev
|
||||
- uv run pytest -m "not integration" -v
|
||||
230
CLAUDE.md
Normal file
230
CLAUDE.md
Normal file
@ -0,0 +1,230 @@
|
||||
## Design Context
|
||||
|
||||
### Users
|
||||
Developers across the full spectrum — solo engineers building side projects, startup teams integrating sandboxed execution into products, and platform/infra engineers at larger organizations running production workloads on Firecracker microVMs. They arrive with context: they know what a process is, what a rootfs is, what a TTY means. The interface must feel at home for all three: approachable enough not to intimidate a hacker, precise enough to earn the trust of a production ops team. Never condescend, never oversimplify. Trust the user to understand what they're looking at.
|
||||
|
||||
**Primary job to be done:** Understand what's running, act on it confidently, and get back to code.
|
||||
|
||||
### Brand Personality
|
||||
**Precise. Warm. Uncompromising.**
|
||||
|
||||
Wrenn is an engineer's favorite tool — built with visible care, not assembled from defaults. It runs real infrastructure (Firecracker microVMs), so the UI should reflect that seriousness without becoming cold or corporate. The warmth comes from the typography and color palette; the precision comes from hierarchy, density, and data fidelity.
|
||||
|
||||
Emotional goal: **in control.** Users leave a session with full confidence in what's running, what happened, and what comes next. Nothing is hidden, nothing is ambiguous.
|
||||
|
||||
### Aesthetic Direction
|
||||
**Dark-only (permanently), industrial-warm, data-forward.**
|
||||
|
||||
No light mode planned. All design decisions should optimize for dark. The near-black-green background palette (`#0a0c0b` through `#2a302d`) reads as "black with intention" — not pitch black (cold) and not charcoal (dated). The sage green accent (`#5e8c58`) is muted and organic, a meaningful departure from the startup-green neon that saturates the developer tool space.
|
||||
|
||||
**Anti-references:**
|
||||
- **Supabase**: avoid the friendly, approachable startup-green energy — too generic, too eager to please
|
||||
- **AWS / GCP consoles**: avoid utility-first density without craft — functional but joyless, visually dated
|
||||
|
||||
**References that capture the right spirit:**
|
||||
- The precision of a well-calibrated instrument
|
||||
- Editorial typography from technical publications
|
||||
- The quiet confidence of tools that don't need to explain themselves
|
||||
|
||||
### Type System
|
||||
Four fonts with strict roles — this is the design system's strongest personality trait and must be respected:
|
||||
|
||||
| Font | CSS Class | Role | When to use |
|
||||
|------|-----------|------|-------------|
|
||||
| **Manrope** (variable, sans) | `font-sans` | UI workhorse | All body copy, nav, labels, buttons, form text |
|
||||
| **Instrument Serif** | `font-serif` | Display / editorial | Page titles (h1), dialog headings, metric values, hero moments |
|
||||
| **JetBrains Mono** (variable) | `font-mono` | Data / code | IDs, timestamps, key prefixes, file paths, terminal output, metrics |
|
||||
| **Alice** | brand wordmark only | Brand wordmark | "Wrenn" in sidebar and login only — nowhere else |
|
||||
|
||||
Instrument Serif at scale creates the signature editorial moments. Mono provides the precision signal for technical data. Never swap these roles.
|
||||
|
||||
**Tracking overrides (app.css):**
|
||||
- `.font-serif` — `letter-spacing: 0.015em` (positive tracking; Instrument Serif reads less condensed at display sizes)
|
||||
- `.font-mono` — `font-variant-numeric: tabular-nums` (numbers align in tables and metric displays)
|
||||
|
||||
**Type scale (root: 87.5% = 14px base):**
|
||||
| Token | Value | Use |
|
||||
|---|---|---|
|
||||
| `--text-display` | 2.571rem (~36px) | Auth section headings |
|
||||
| `--text-page` | 2rem (~28px) | Page h1 titles |
|
||||
| `--text-heading` | 1.429rem (~20px) | Dialog headings, empty states |
|
||||
| `--text-body` | 1rem (~14px) | Primary body, buttons, inputs |
|
||||
| `--text-ui` | 0.929rem (~13px) | Nav labels, table cells |
|
||||
| `--text-meta` | 0.857rem (~12px) | Key prefixes, minor info |
|
||||
| `--text-label` | 0.786rem (~11px) | Uppercase section labels |
|
||||
| `--text-badge` | 0.714rem (~10px) | Live badges, tiny indicators |
|
||||
|
||||
### Color System
|
||||
|
||||
All values are CSS custom properties in `frontend/src/app.css`.
|
||||
|
||||
**Backgrounds (6-step near-black-green scale):**
|
||||
| Token | Value | Use |
|
||||
|---|---|---|
|
||||
| `--color-bg-0` | `#0a0c0b` | Page base, sidebar deepest layer |
|
||||
| `--color-bg-1` | `#0f1211` | Sidebar surface |
|
||||
| `--color-bg-2` | `#141817` | Card backgrounds |
|
||||
| `--color-bg-3` | `#1a1e1c` | Table headers, elevated surfaces |
|
||||
| `--color-bg-4` | `#212624` | Hover states, inputs |
|
||||
| `--color-bg-5` | `#2a302d` | Highlighted items, selected rows |
|
||||
|
||||
**Text (5-level hierarchy):**
|
||||
| Token | Value | Use |
|
||||
|---|---|---|
|
||||
| `--color-text-bright` | `#eae7e2` | H1s, dialog headings |
|
||||
| `--color-text-primary` | `#d0cdc6` | Body copy, primary labels |
|
||||
| `--color-text-secondary` | `#9b9790` | Secondary labels, descriptions |
|
||||
| `--color-text-tertiary` | `#6b6862` | Hints, placeholders |
|
||||
| `--color-text-muted` | `#454340` | Dividers as text, ultra-subtle |
|
||||
|
||||
**Accent (sage green — use sparingly, must feel earned):**
|
||||
| Token | Value | Use |
|
||||
|---|---|---|
|
||||
| `--color-accent` | `#5e8c58` | Primary CTA, live indicators, focus rings, active nav |
|
||||
| `--color-accent-mid` | `#89a785` | Hover accent text |
|
||||
| `--color-accent-bright` | `#a4c89f` | Accent on dark backgrounds |
|
||||
| `--color-accent-glow` | `rgba(94,140,88,0.07)` | Subtle tinted backgrounds |
|
||||
| `--color-accent-glow-mid` | `rgba(94,140,88,0.14)` | Hover tint on accent items |
|
||||
|
||||
**Status semantics:**
|
||||
| Token | Value | Use |
|
||||
|---|---|---|
|
||||
| `--color-amber` | `#d4a73c` | Warning, paused state |
|
||||
| `--color-red` | `#cf8172` | Error, destructive actions |
|
||||
| `--color-blue` | `#5a9fd4` | Info, neutral system states |
|
||||
|
||||
**Borders:** `--color-border` (`#1f2321`) default; `--color-border-mid` (`#2a2f2c`) for inputs/hover.
|
||||
|
||||
### Component Patterns
|
||||
|
||||
**Buttons:**
|
||||
- Primary: solid sage green (`--color-accent`), hover brightness boost + micro-lift (`-translate-y-px`)
|
||||
- Secondary: bordered (`--color-border-mid`), text transitions to accent on hover
|
||||
- Danger: red text + subtle red background on hover
|
||||
- All: `transition-all duration-150`
|
||||
|
||||
**Inputs:**
|
||||
- Border `--color-border`, background `--color-bg-2`; focus transitions border and icon to accent
|
||||
- Group focus pattern: `group` wrapper + `group-focus-within:text-[var(--color-accent)]` on icon
|
||||
|
||||
**Tables / data lists:**
|
||||
- Grid layout; header `bg-3` + uppercase `--text-label`; row hover `hover:bg-[var(--color-bg-3)]`
|
||||
- Status stripe: left border color matches sandbox state
|
||||
|
||||
**Status indicators:** Running = animated ping + sage green dot; Paused = amber dot; Stopped = muted gray. Color is never the sole differentiator.
|
||||
|
||||
**Modals & dialogs:** Border + shadow only — no accent gradient bars/strips. `fadeUp` 0.35s entrance.
|
||||
|
||||
**Empty states:** Large icon with glow, Instrument Serif heading, secondary body text, CTA below, `iconFloat` 4s animation.
|
||||
|
||||
**Animations (always respect `prefers-reduced-motion`):** `fadeUp` (entrance), `status-ping` (live indicator), `iconFloat` (empty states), `spin-once` (refresh), staggered `animation-delay` on lists.
|
||||
|
||||
### Design Principles
|
||||
|
||||
1. **Precision over friendliness.** Every element earns its place. Wrenn doesn't need to tell you it's developer-friendly — that should be self-evident from the quality of the information architecture.
|
||||
|
||||
2. **Density with breathing room.** Data-forward doesn't mean cramped. Strategic whitespace creates calm hierarchy within dense contexts. Sections breathe; rows don't waste space.
|
||||
|
||||
3. **Industrial warmth.** The serif + mono + warm-black combination prevents sterility. This is a forge, not a gallery. The warmth is in the details, not the primary colors.
|
||||
|
||||
4. **Legible at speed.** Users scan dashboards in seconds. Strong typographic contrast (serif h1, mono IDs, sans body), consistent patterns, and predictable placement let users orientate instantly without reading everything.
|
||||
|
||||
5. **Craft signals trust.** For infrastructure that runs production code, the quality of the UI is a proxy for the quality of the product. Pixel-level decisions matter. Polish is not decoration — it's a trust signal.
|
||||
|
||||
<!-- code-review-graph MCP tools -->
|
||||
## MCP Tools: code-review-graph
|
||||
|
||||
**IMPORTANT: This project has a knowledge graph. ALWAYS use the
|
||||
code-review-graph MCP tools BEFORE using Grep/Glob/Read to explore
|
||||
the codebase.** The graph is faster, cheaper (fewer tokens), and gives
|
||||
you structural context (callers, dependents, test coverage) that file
|
||||
scanning cannot.
|
||||
|
||||
### When to use graph tools FIRST
|
||||
|
||||
- **Exploring code**: `semantic_search_nodes` or `query_graph` instead of Grep
|
||||
- **Understanding impact**: `get_impact_radius` instead of manually tracing imports
|
||||
- **Code review**: `detect_changes` + `get_review_context` instead of reading entire files
|
||||
- **Finding relationships**: `query_graph` with callers_of/callees_of/imports_of/tests_for
|
||||
- **Architecture questions**: `get_architecture_overview` + `list_communities`
|
||||
|
||||
Fall back to Grep/Glob/Read **only** when the graph doesn't cover what you need.
|
||||
|
||||
### Key Tools
|
||||
|
||||
| Tool | Use when |
|
||||
|------|----------|
|
||||
| `detect_changes` | Reviewing code changes — gives risk-scored analysis |
|
||||
| `get_review_context` | Need source snippets for review — token-efficient |
|
||||
| `get_impact_radius` | Understanding blast radius of a change |
|
||||
| `get_affected_flows` | Finding which execution paths are impacted |
|
||||
| `query_graph` | Tracing callers, callees, imports, tests, dependencies |
|
||||
| `semantic_search_nodes` | Finding functions/classes by name or keyword |
|
||||
| `get_architecture_overview` | Understanding high-level codebase structure |
|
||||
| `refactor_tool` | Planning renames, finding dead code |
|
||||
|
||||
### Workflow
|
||||
|
||||
1. The graph auto-updates on file changes (via hooks).
|
||||
2. Use `detect_changes` for code review.
|
||||
3. Use `get_affected_flows` to understand impact.
|
||||
4. Use `query_graph` pattern="tests_for" to check coverage.
|
||||
|
||||
## Code Runner Module
|
||||
|
||||
`wrenn.code_runner` — stateful code execution capsule via persistent
|
||||
Jupyter kernel.
|
||||
|
||||
- **Module path:** `wrenn.code_runner` (canonical). The old path
|
||||
`wrenn.code_interpreter` is a deprecation alias that emits a
|
||||
`FutureWarning` on import; do not introduce new uses.
|
||||
- **Defaults:** template `code-runner-beta`, kernelspec `wrenn`.
|
||||
Both overridable via `Capsule(template=..., kernel=...)`.
|
||||
- **Kernel reuse:** `_ensure_kernel` lists `/api/kernels`, reuses the
|
||||
first kernel whose `name` matches the configured kernelspec, else
|
||||
POSTs `{"name": <kernel>}` to create one. Matching by name (not just
|
||||
"any kernel") is intentional — multiple kernelspecs may coexist on
|
||||
the same Jupyter.
|
||||
- **Lifecycle invariant:** the constructor sets `_kernel_id`,
|
||||
`_kernel_name`, `_proxy_client` to safe defaults *before* calling
|
||||
`super().__init__`. `__del__` must never assume construction
|
||||
completed. Async `__del__` only drops the reference — the proxy
|
||||
`httpx.AsyncClient` must be closed via `await close()` or
|
||||
`async with`.
|
||||
|
||||
## Client Config
|
||||
|
||||
`WrennClient` / `AsyncWrennClient` accept:
|
||||
- `api_key` — falls back to `WRENN_API_KEY`.
|
||||
- `base_url` — falls back to `WRENN_BASE_URL`, then `DEFAULT_BASE_URL`
|
||||
(`https://app.wrenn.dev/api`).
|
||||
- `proxy_domain` — host suffix for capsule proxy URLs
|
||||
(`{port}-{capsule_id}.<domain>`). Resolution:
|
||||
1. explicit `proxy_domain=` kwarg
|
||||
2. `WRENN_PROXY_DOMAIN` env
|
||||
3. `wrenn.dev` when `base_url` host == `app.wrenn.dev` exactly
|
||||
4. else `base_url` host (with port) verbatim
|
||||
Exact match in step 3 is intentional: staging/other Wrenn envs keep
|
||||
their host so they don't accidentally collapse to prod `wrenn.dev`.
|
||||
- `timeout` — `httpx.Timeout | float | None`. Default
|
||||
`httpx.Timeout(30.0, connect=10.0)`. Helper `_resolve_timeout`
|
||||
centralizes the float-or-Timeout coercion.
|
||||
|
||||
`_build_proxy_url` / `_build_http_proxy_url` in `wrenn.capsule` now take
|
||||
an optional `proxy_domain` arg. When omitted they fall back to the
|
||||
`base_url` host (legacy behavior, preserved for direct callers/tests).
|
||||
Production call sites pass `self._client._proxy_domain`.
|
||||
|
||||
### Tests
|
||||
|
||||
- `tests/test_code_runner_unit.py` — pure unit tests (respx + mocked
|
||||
WebSocket). Covers `Result.from_bundle`, MIME unpacking,
|
||||
quote-stripping, `Execution.text`, kernel reuse vs create, retry on
|
||||
5xx, 4xx propagation, ctor-failure-safe `__del__`, deprecation
|
||||
alias.
|
||||
- `tests/test_code_runner_e2e.py` — live integration tests (marked
|
||||
`integration`, skipped without `WRENN_API_KEY`). Covers stateful
|
||||
execution, exceptions, callbacks, rich outputs (HTML, matplotlib,
|
||||
pandas), async variant, isolation between capsules, and the
|
||||
deprecated `code_interpreter` import path.
|
||||
- Run both: `make test-code-runner`.
|
||||
20
LICENSE
20
LICENSE
@ -1,18 +1,18 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 wrenn
|
||||
Copyright (c) 2026 M/S Omukk, Bangladesh
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||
portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
||||
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
||||
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
27
Makefile
27
Makefile
@ -1,8 +1,8 @@
|
||||
# Makefile
|
||||
.PHONY: generate
|
||||
.PHONY: generate lint test check test-integration test-code-runner
|
||||
|
||||
# Variables
|
||||
SPEC_URL = "https://git.omukk.dev/wrenn/sandbox/raw/branch/main/internal/api/openapi.yaml"
|
||||
SPEC_URL = "https://raw.githubusercontent.com/wrennhq/wrenn/refs/heads/main/internal/api/openapi.yaml"
|
||||
SPEC_PATH = "api/openapi.yaml"
|
||||
|
||||
generate:
|
||||
@ -21,4 +21,25 @@ generate:
|
||||
--use-schema-description \
|
||||
--target-python-version 3.13 \
|
||||
--use-annotated \
|
||||
--openapi-scopes schemas
|
||||
--openapi-scopes schemas \
|
||||
--formatters ruff-format ruff-check \
|
||||
--input-file-type openapi
|
||||
|
||||
lint:
|
||||
uv run ruff check src/
|
||||
uv run ruff format --check src/
|
||||
|
||||
test:
|
||||
uv run pytest tests/test_client.py tests/test_code_runner_unit.py -v
|
||||
|
||||
test-integration:
|
||||
uv run pytest tests/ -v -m "integration or not integration" --ignore=tests/test_code_runner_e2e.py --ignore=tests/test_code_runner_unit.py
|
||||
|
||||
test-code-runner:
|
||||
uv run pytest tests/test_code_runner_unit.py tests/test_code_runner_e2e.py -v -m "integration or not integration"
|
||||
|
||||
check: lint test
|
||||
|
||||
gen-docs:
|
||||
mkdir -p docs
|
||||
uv run pydoc-markdown > docs/reference.md
|
||||
|
||||
679
README.md
679
README.md
@ -1,3 +1,678 @@
|
||||
# python-sdk
|
||||
# Wrenn Python SDK
|
||||
|
||||
Python SDK for wrenn
|
||||
Python client for the [Wrenn](https://wrenn.dev) microVM platform. Create isolated capsules, execute commands, manage files, run interactive terminals, and execute persistent code -- all from Python.
|
||||
|
||||
Designed as a drop-in replacement for [e2b](https://e2b.dev). If you're migrating, just swap your imports.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install wrenn
|
||||
```
|
||||
|
||||
Requires Python 3.13+.
|
||||
|
||||
## Authentication
|
||||
|
||||
Set the `WRENN_API_KEY` environment variable:
|
||||
|
||||
```bash
|
||||
export WRENN_API_KEY="wrn_your_api_key_here"
|
||||
```
|
||||
|
||||
Optionally override the API base URL:
|
||||
|
||||
```bash
|
||||
export WRENN_BASE_URL="https://app.wrenn.dev/api" # default
|
||||
```
|
||||
|
||||
For self-hosted deployments you can also override the capsule proxy domain
|
||||
(used to build `{port}-{capsule_id}.<domain>` URLs returned by
|
||||
`Capsule.get_url`):
|
||||
|
||||
```bash
|
||||
export WRENN_PROXY_DOMAIN="wrenn.example.com"
|
||||
```
|
||||
|
||||
Resolution order: explicit `proxy_domain=` kwarg → `WRENN_PROXY_DOMAIN` env →
|
||||
`wrenn.dev` when `base_url` is the default `app.wrenn.dev` host, else the
|
||||
`base_url` host (with port) verbatim.
|
||||
|
||||
You can also pass credentials directly:
|
||||
|
||||
```python
|
||||
from wrenn import WrennClient, Capsule
|
||||
|
||||
# WrennClient also accepts a timeout (httpx.Timeout or float seconds).
|
||||
# Default: 30s read/write/pool, 10s connect.
|
||||
client = WrennClient(
|
||||
api_key="wrn_...",
|
||||
base_url="https://...",
|
||||
proxy_domain="wrenn.example.com", # optional override
|
||||
timeout=30.0, # optional override
|
||||
)
|
||||
|
||||
capsule = Capsule(api_key="wrn_...", base_url="https://...")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Wrenn Capsules
|
||||
|
||||
### Quick Start
|
||||
|
||||
```python
|
||||
from wrenn import Capsule
|
||||
|
||||
# Create a capsule (reads WRENN_API_KEY from env)
|
||||
with Capsule(template="minimal") as capsule:
|
||||
result = capsule.commands.run("echo hello")
|
||||
print(result.stdout) # "hello\n"
|
||||
```
|
||||
|
||||
### Creating Capsules
|
||||
|
||||
```python
|
||||
from wrenn import Capsule
|
||||
|
||||
# Direct construction (creates immediately)
|
||||
capsule = Capsule()
|
||||
capsule = Capsule(template="base-python", vcpus=2, memory_mb=1024, timeout=300)
|
||||
|
||||
# With auto-wait (blocks until capsule is running)
|
||||
capsule = Capsule(template="minimal", wait=True)
|
||||
|
||||
# Via factory classmethod
|
||||
capsule = Capsule.create(template="minimal", wait=True)
|
||||
```
|
||||
|
||||
### Context Manager
|
||||
|
||||
Use capsules as context managers for automatic cleanup (destroys capsule on exit):
|
||||
|
||||
```python
|
||||
with Capsule(template="minimal", wait=True) as capsule:
|
||||
capsule.commands.run("echo hello")
|
||||
# capsule is automatically destroyed
|
||||
```
|
||||
|
||||
### Connecting to Existing Capsules
|
||||
|
||||
Attach to a running capsule by ID. If it's paused, it will be resumed automatically:
|
||||
|
||||
```python
|
||||
capsule = Capsule.connect("cl-abc123")
|
||||
result = capsule.commands.run("echo still running")
|
||||
```
|
||||
|
||||
For code runner capsules:
|
||||
|
||||
```python
|
||||
from wrenn.code_runner import Capsule as CodeCapsule
|
||||
|
||||
capsule = CodeCapsule.connect("cl-abc123")
|
||||
result = capsule.run_code("print('reconnected')")
|
||||
```
|
||||
|
||||
### Lifecycle Management
|
||||
|
||||
```python
|
||||
# Instance methods
|
||||
capsule.pause()
|
||||
capsule.resume()
|
||||
capsule.destroy()
|
||||
capsule.ping() # reset inactivity timer
|
||||
capsule.wait_ready() # block until running
|
||||
|
||||
info = capsule.get_info()
|
||||
print(info.status) # "running"
|
||||
print(capsule.is_running()) # True
|
||||
|
||||
# Static methods (no instance needed)
|
||||
Capsule.destroy("cl-abc123", api_key="wrn_...")
|
||||
Capsule.pause("cl-abc123")
|
||||
Capsule.resume("cl-abc123")
|
||||
info = Capsule.get_info("cl-abc123")
|
||||
|
||||
# List all capsules
|
||||
capsules = Capsule.list()
|
||||
```
|
||||
|
||||
### Command Execution
|
||||
|
||||
Commands are accessed via `capsule.commands`:
|
||||
|
||||
```python
|
||||
# Foreground (blocks until complete)
|
||||
result = capsule.commands.run("python -c 'print(42)'")
|
||||
print(result.stdout) # "42\n"
|
||||
print(result.stderr) # ""
|
||||
print(result.exit_code) # 0
|
||||
print(result.duration_ms) # 35
|
||||
|
||||
# With options
|
||||
result = capsule.commands.run(
|
||||
"python train.py",
|
||||
timeout=120,
|
||||
envs={"CUDA_VISIBLE_DEVICES": "0"},
|
||||
cwd="/app",
|
||||
)
|
||||
|
||||
# Background process
|
||||
handle = capsule.commands.run("python server.py", background=True)
|
||||
print(handle.pid) # 1234
|
||||
print(handle.tag) # "exec-abc123"
|
||||
```
|
||||
|
||||
#### Streaming Output
|
||||
|
||||
```python
|
||||
import sys
|
||||
|
||||
# Stream a new command
|
||||
for event in capsule.commands.stream("python", args=["-u", "train.py"]):
|
||||
match event.type:
|
||||
case "start":
|
||||
print(f"PID: {event.pid}")
|
||||
case "stdout":
|
||||
print(event.data, end="")
|
||||
case "stderr":
|
||||
print(event.data, end="", file=sys.stderr)
|
||||
case "exit":
|
||||
print(f"\nExited with code {event.exit_code}")
|
||||
|
||||
# Connect to a running background process
|
||||
for event in capsule.commands.connect(handle.pid):
|
||||
match event.type:
|
||||
case "start":
|
||||
print(f"PID: {event.pid}")
|
||||
case "stdout":
|
||||
print(event.data, end="")
|
||||
```
|
||||
|
||||
#### Process Management
|
||||
|
||||
```python
|
||||
# List running processes
|
||||
for proc in capsule.commands.list():
|
||||
print(proc.pid, proc.cmd, proc.tag)
|
||||
|
||||
# Kill a process
|
||||
capsule.commands.kill(pid=1234)
|
||||
```
|
||||
|
||||
### Filesystem
|
||||
|
||||
Files are accessed via `capsule.files`:
|
||||
|
||||
```python
|
||||
# Write and read files
|
||||
capsule.files.write("/app/main.py", "print('hello')")
|
||||
content = capsule.files.read("/app/main.py") # str
|
||||
raw = capsule.files.read_bytes("/app/main.py") # bytes
|
||||
|
||||
# Check existence
|
||||
capsule.files.exists("/app/main.py") # True
|
||||
|
||||
# List directory
|
||||
entries = capsule.files.list("/home/user", depth=1)
|
||||
# FileEntry has: name, type (file/dir), size, modified_at
|
||||
for entry in entries:
|
||||
print(entry.name, entry.type, entry.size)
|
||||
|
||||
# Create directory
|
||||
capsule.files.make_dir("/app/data")
|
||||
|
||||
# Remove file or directory
|
||||
capsule.files.remove("/app/old_data")
|
||||
```
|
||||
|
||||
#### Streaming (Large Files)
|
||||
|
||||
```python
|
||||
# Streaming upload
|
||||
def chunks():
|
||||
yield b"chunk1"
|
||||
yield b"chunk2"
|
||||
|
||||
capsule.files.upload_stream("/data/large.bin", chunks())
|
||||
|
||||
# Streaming download
|
||||
for chunk in capsule.files.download_stream("/data/large.bin"):
|
||||
process(chunk)
|
||||
```
|
||||
|
||||
### Git
|
||||
|
||||
Git operations are accessed via `capsule.git`. All commands execute the real `git` binary inside the capsule:
|
||||
|
||||
```python
|
||||
# Initialize a repo
|
||||
capsule.git.init("/app", initial_branch="main")
|
||||
|
||||
# Configure user
|
||||
capsule.git.configure_user("Alice", "alice@example.com", cwd="/app")
|
||||
|
||||
# Stage and commit
|
||||
capsule.git.add(all=True, cwd="/app")
|
||||
capsule.git.commit("initial commit", cwd="/app")
|
||||
|
||||
# Check status
|
||||
status = capsule.git.status(cwd="/app")
|
||||
print(status.branch) # "main"
|
||||
print(status.is_clean) # True
|
||||
for f in status.files:
|
||||
print(f.path, f.index_status, f.work_tree_status)
|
||||
|
||||
# Branches
|
||||
branches = capsule.git.branches(cwd="/app")
|
||||
capsule.git.create_branch("feature", cwd="/app")
|
||||
capsule.git.checkout_branch("main", cwd="/app")
|
||||
capsule.git.delete_branch("feature", cwd="/app")
|
||||
```
|
||||
|
||||
#### Clone with Authentication
|
||||
|
||||
```python
|
||||
# Clone a private repo (credentials are stripped from remote URL after clone)
|
||||
capsule.git.clone(
|
||||
"https://github.com/org/repo.git",
|
||||
username="user",
|
||||
password="ghp_token",
|
||||
cwd="/app",
|
||||
)
|
||||
|
||||
# Push/pull with inline credentials (temporarily embedded, then restored)
|
||||
capsule.git.push("origin", "main", username="user", password="ghp_token", cwd="/app")
|
||||
capsule.git.pull("origin", "main", username="user", password="ghp_token", cwd="/app")
|
||||
```
|
||||
|
||||
#### Configuration and Remotes
|
||||
|
||||
```python
|
||||
capsule.git.set_config("core.autocrlf", "false", cwd="/app")
|
||||
value = capsule.git.get_config("user.name", cwd="/app") # str | None
|
||||
|
||||
capsule.git.remote_add("upstream", "https://github.com/org/repo.git", cwd="/app")
|
||||
url = capsule.git.remote_get("origin", cwd="/app") # str | None
|
||||
|
||||
# Reset and restore
|
||||
capsule.git.reset(mode="hard", ref="HEAD~1", cwd="/app")
|
||||
capsule.git.restore(["file.txt"], staged=True, cwd="/app")
|
||||
```
|
||||
|
||||
#### Persistent Credential Store
|
||||
|
||||
For workflows that need repeated authenticated operations, you can persist credentials via the git credential store:
|
||||
|
||||
```python
|
||||
capsule.git.dangerously_authenticate(
|
||||
username="user",
|
||||
password="ghp_token",
|
||||
host="github.com",
|
||||
protocol="https",
|
||||
)
|
||||
```
|
||||
|
||||
> **Warning:** Credentials are written in plaintext inside the capsule and are accessible to any process running there. Prefer per-operation `username`/`password` on `clone`, `push`, and `pull` instead.
|
||||
|
||||
Git errors raise `GitCommandError` (or `GitAuthError` for authentication failures), both inheriting from `GitError`:
|
||||
|
||||
```python
|
||||
from wrenn import GitCommandError, GitAuthError
|
||||
|
||||
try:
|
||||
capsule.git.push("origin", "main", username="user", password="bad", cwd="/app")
|
||||
except GitAuthError as e:
|
||||
print(e.stderr)
|
||||
print(e.exit_code)
|
||||
```
|
||||
|
||||
### Interactive Terminal (PTY)
|
||||
|
||||
```python
|
||||
import sys
|
||||
|
||||
with capsule.pty(cmd="/bin/bash", cols=80, rows=24, cwd="/home/user") as term:
|
||||
term.write(b"ls -la\n")
|
||||
for event in term:
|
||||
if event.type == "output":
|
||||
sys.stdout.buffer.write(event.data)
|
||||
elif event.type == "exit":
|
||||
break
|
||||
|
||||
# Reconnect to an existing session
|
||||
with capsule.pty_connect(term.tag) as term:
|
||||
term.write(b"echo reconnected\n")
|
||||
```
|
||||
|
||||
**PtySession methods:**
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `write(data: bytes)` | Send raw bytes to stdin |
|
||||
| `resize(cols, rows)` | Resize the terminal |
|
||||
| `kill()` | Send SIGKILL to the process |
|
||||
| `tag` | Session tag (after `started` event) |
|
||||
| `pid` | Process PID (after `started` event) |
|
||||
|
||||
### Proxy URL
|
||||
|
||||
Access services running inside a capsule:
|
||||
|
||||
```python
|
||||
url = capsule.get_url(8080)
|
||||
# "wss://8080-cl-abc123.app.wrenn.dev"
|
||||
```
|
||||
|
||||
### Snapshots
|
||||
|
||||
Create reusable templates from running capsules:
|
||||
|
||||
```python
|
||||
template = capsule.create_snapshot(name="my-template", overwrite=True)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Code Runner
|
||||
|
||||
The `wrenn.code_runner` module provides a specialized capsule for stateful code execution via a persistent Jupyter kernel. Defaults to the `code-runner-beta` template and the `wrenn` Jupyter kernelspec.
|
||||
|
||||
> The legacy module path `wrenn.code_interpreter` still works but emits a `FutureWarning` on import. Use `wrenn.code_runner`.
|
||||
|
||||
### Quick Start
|
||||
|
||||
```python
|
||||
from wrenn.code_runner import Capsule
|
||||
|
||||
with Capsule(wait=True) as capsule:
|
||||
result = capsule.run_code("print('hello')")
|
||||
print("".join(result.logs.stdout)) # "hello\n"
|
||||
```
|
||||
|
||||
### Stateful Execution
|
||||
|
||||
Variables, imports, and function definitions persist across `run_code` calls:
|
||||
|
||||
```python
|
||||
from wrenn.code_runner import Capsule
|
||||
|
||||
with Capsule(wait=True) as capsule:
|
||||
capsule.run_code("x = 42")
|
||||
result = capsule.run_code("x * 2")
|
||||
print(result.text) # "84"
|
||||
|
||||
capsule.run_code("import math")
|
||||
result = capsule.run_code("math.pi")
|
||||
print(result.text) # "3.141592653589793"
|
||||
|
||||
capsule.run_code("def greet(name): return f'hello {name}'")
|
||||
result = capsule.run_code("greet('world')")
|
||||
print(result.text) # "hello world"
|
||||
```
|
||||
|
||||
The `text` property returns the `text/plain` value of the main `execute_result` (the last expression in the cell). Printed output goes to `result.logs.stdout` instead.
|
||||
|
||||
### Error Handling in Code
|
||||
|
||||
```python
|
||||
result = capsule.run_code("1 / 0")
|
||||
print(result.error.name) # "ZeroDivisionError"
|
||||
print(result.error.value) # "division by zero"
|
||||
print(result.error.traceback) # full traceback string
|
||||
```
|
||||
|
||||
### Rich Output
|
||||
|
||||
Each call to `display()`, `plt.show()`, or similar produces a `Result` in `execution.results`. Known MIME types are unpacked into named fields:
|
||||
|
||||
```python
|
||||
result = capsule.run_code("""
|
||||
import matplotlib.pyplot as plt
|
||||
plt.plot([1, 2, 3])
|
||||
plt.show()
|
||||
""")
|
||||
for r in result.results:
|
||||
if r.png:
|
||||
print(f"Got PNG image ({len(r.png)} bytes base64)")
|
||||
print(r.formats()) # e.g. ["text", "png"]
|
||||
```
|
||||
|
||||
### Streaming Callbacks
|
||||
|
||||
```python
|
||||
capsule.run_code(
|
||||
code,
|
||||
on_result=lambda r: print("result:", r.formats()),
|
||||
on_stdout=lambda text: print("stdout:", text),
|
||||
on_stderr=lambda text: print("stderr:", text),
|
||||
on_error=lambda err: print(f"error: {err.name}: {err.value}"),
|
||||
)
|
||||
```
|
||||
|
||||
### Custom Templates and Kernels
|
||||
|
||||
By default, the `code-runner-beta` template and the `wrenn` Jupyter kernelspec are used. Override either:
|
||||
|
||||
```python
|
||||
capsule = Capsule(
|
||||
template="my-custom-jupyter-template",
|
||||
kernel="python3",
|
||||
wait=True,
|
||||
)
|
||||
result = capsule.run_code("print('running on custom template')")
|
||||
```
|
||||
|
||||
`Capsule` reuses the first kernel matching the requested `kernel` name on the Jupyter server and creates one if none exists.
|
||||
|
||||
### Execution Model
|
||||
|
||||
`run_code()` returns an `Execution` object:
|
||||
|
||||
| Field | Type | Description |
|
||||
|-------|------|-------------|
|
||||
| `results` | `list[Result]` | All rich outputs (charts, images, expression values) |
|
||||
| `logs` | `Logs` | `.stdout: list[str]` and `.stderr: list[str]` chunks |
|
||||
| `error` | `ExecutionError \| None` | `.name`, `.value`, `.traceback` |
|
||||
| `execution_count` | `int \| None` | Jupyter cell execution counter |
|
||||
| `timed_out` | `bool` | ``True`` when execution was cut short by the timeout |
|
||||
| `text` | `str \| None` | (property) `text/plain` of the main `execute_result` |
|
||||
|
||||
Each `Result` has typed MIME fields: `text`, `html`, `markdown`, `svg`, `png`, `jpeg`, `gif`, `pdf`, `latex`, `json`, `javascript`, `plotly`, plus `extra` for unknown types. The `text` field is Jupyter's `text/plain` bundle verbatim — the Python `repr()` of the cell's last expression. So `run_code("'hi'").text` is `"'hi'"` (with quotes), and `run_code("42").text` is `"42"`. This preserves the distinction between the string `'2'` and the int `2`.
|
||||
|
||||
### Code Runner + Commands/Files
|
||||
|
||||
The code runner capsule inherits all standard capsule features:
|
||||
|
||||
```python
|
||||
from wrenn.code_runner import Capsule
|
||||
|
||||
with Capsule(wait=True) as capsule:
|
||||
# Use run_code for Jupyter execution
|
||||
capsule.run_code("import pandas as pd; df = pd.DataFrame({'a': [1,2,3]})")
|
||||
capsule.run_code("df.to_csv('/tmp/data.csv', index=False)")
|
||||
|
||||
# Use standard file operations
|
||||
content = capsule.files.read("/tmp/data.csv")
|
||||
print(content)
|
||||
|
||||
# Use standard command execution
|
||||
result = capsule.commands.run("wc -l /tmp/data.csv")
|
||||
print(result.stdout)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Async Support
|
||||
|
||||
All operations have async variants via `AsyncCapsule`:
|
||||
|
||||
### Async Capsule
|
||||
|
||||
```python
|
||||
from wrenn import AsyncCapsule
|
||||
|
||||
async with await AsyncCapsule.create(template="minimal", wait=True) as capsule:
|
||||
result = await capsule.commands.run("echo hello")
|
||||
print(result.stdout)
|
||||
|
||||
await capsule.files.write("/app/file.txt", "data")
|
||||
entries = await capsule.files.list("/app")
|
||||
|
||||
await capsule.pause()
|
||||
await capsule.resume()
|
||||
```
|
||||
|
||||
### Async Code Runner
|
||||
|
||||
```python
|
||||
from wrenn.code_runner import AsyncCapsule
|
||||
|
||||
async with await AsyncCapsule.create(wait=True) as capsule:
|
||||
result = await capsule.run_code("2 + 2")
|
||||
print(result.text) # "4"
|
||||
```
|
||||
|
||||
### Async PTY
|
||||
|
||||
```python
|
||||
async with capsule.pty(cmd="/bin/bash") as term:
|
||||
await term.write(b"ls -la\n")
|
||||
async for event in term:
|
||||
if event.type == "output":
|
||||
sys.stdout.buffer.write(event.data)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Error Handling
|
||||
|
||||
The SDK maps server error codes to typed exceptions:
|
||||
|
||||
```python
|
||||
from wrenn import (
|
||||
WrennError,
|
||||
WrennValidationError, # 400
|
||||
WrennAuthenticationError, # 401
|
||||
WrennForbiddenError, # 403
|
||||
WrennNotFoundError, # 404
|
||||
WrennConflictError, # 409
|
||||
WrennHostHasCapsulesError, # 409 (host has running capsules)
|
||||
WrennInternalError, # 500
|
||||
WrennAgentError, # 502
|
||||
WrennHostUnavailableError, # 503
|
||||
)
|
||||
|
||||
try:
|
||||
Capsule.get_info("nonexistent")
|
||||
except WrennNotFoundError as e:
|
||||
print(e.code) # "not_found"
|
||||
print(e.message) # "capsule not found"
|
||||
print(e.status_code) # 404
|
||||
```
|
||||
|
||||
All exceptions inherit from `WrennError` and expose `.code`, `.message`, and `.status_code`.
|
||||
|
||||
---
|
||||
|
||||
## Migrating from e2b
|
||||
|
||||
Replace your imports:
|
||||
|
||||
```python
|
||||
# Before
|
||||
from e2b import Sandbox
|
||||
sandbox = Sandbox()
|
||||
|
||||
# After
|
||||
from wrenn import Capsule
|
||||
capsule = Capsule()
|
||||
```
|
||||
|
||||
For code interpreter:
|
||||
|
||||
```python
|
||||
# Before
|
||||
from e2b_code_interpreter import Sandbox
|
||||
sandbox = Sandbox()
|
||||
result = sandbox.run_code("print('hello')")
|
||||
|
||||
# After
|
||||
from wrenn.code_interpreter import Capsule
|
||||
capsule = Capsule()
|
||||
result = capsule.run_code("print('hello')")
|
||||
```
|
||||
|
||||
The `Sandbox` name is available as a deprecated alias in both modules:
|
||||
|
||||
```python
|
||||
from wrenn import Sandbox # works, emits FutureWarning
|
||||
from wrenn.code_interpreter import Sandbox # works, emits FutureWarning
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Low-Level Client
|
||||
|
||||
For direct API access, use `WrennClient` / `AsyncWrennClient`:
|
||||
|
||||
```python
|
||||
from wrenn import WrennClient
|
||||
|
||||
with WrennClient(api_key="wrn_...") as client:
|
||||
capsule = client.capsules.create(template="minimal")
|
||||
client.capsules.pause(capsule.id)
|
||||
client.capsules.resume(capsule.id)
|
||||
client.capsules.ping(capsule.id)
|
||||
client.capsules.destroy(capsule.id)
|
||||
|
||||
# Snapshots
|
||||
template = client.snapshots.create(capsule_id="cl-abc", name="my-snap")
|
||||
templates = client.snapshots.list(type="custom") # optional type filter
|
||||
client.snapshots.delete("my-snap")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Development
|
||||
|
||||
This project uses [uv](https://docs.astral.sh/uv/) for dependency management.
|
||||
|
||||
```bash
|
||||
# Install dependencies
|
||||
uv sync
|
||||
|
||||
# Run linting
|
||||
make lint
|
||||
|
||||
# Run unit tests
|
||||
make test
|
||||
|
||||
# Run all tests (including integration)
|
||||
make test-integration
|
||||
```
|
||||
|
||||
### Running Integration Tests
|
||||
|
||||
Integration tests require a live Wrenn server. Set credentials via environment or a `.env` file at the project root:
|
||||
|
||||
```bash
|
||||
# Option 1: environment variable
|
||||
export WRENN_API_KEY="wrn_..."
|
||||
|
||||
# Option 2: .env file
|
||||
echo 'WRENN_API_KEY=wrn_...' > .env
|
||||
```
|
||||
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
make test-integration
|
||||
```
|
||||
|
||||
Tests are automatically skipped when `WRENN_API_KEY` is not available.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
3475
api/openapi.yaml
3475
api/openapi.yaml
File diff suppressed because it is too large
Load Diff
4434
docs/reference.md
Normal file
4434
docs/reference.md
Normal file
File diff suppressed because it is too large
Load Diff
12
pydoc-markdown.yml
Normal file
12
pydoc-markdown.yml
Normal file
@ -0,0 +1,12 @@
|
||||
loaders:
|
||||
- type: python
|
||||
search_path: [src]
|
||||
|
||||
processors:
|
||||
- type: google # Use Google-style docstring parser
|
||||
- type: filter
|
||||
- type: crossref
|
||||
|
||||
renderer:
|
||||
type: markdown
|
||||
escape_html_in_docstring: false
|
||||
@ -1,14 +1,31 @@
|
||||
[project]
|
||||
name = "wrenn"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
version = "0.2.0"
|
||||
description = "Python SDK for Wrenn"
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
license-files = ["LICENSE"]
|
||||
authors = [
|
||||
{ name = "Tasnim Kabir Sadik", email = "tksadik92@gmail.com" }
|
||||
{ name = "Rafeed M. Bhuiyan", email = "rafeed@omukk.dev" },
|
||||
{ name = "Tasnim Kabir Sadik", email = "tksadik@omukk.dev" },
|
||||
]
|
||||
requires-python = ">=3.13"
|
||||
keywords = ["wrenn"]
|
||||
classifiers = [
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.13",
|
||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||
"Typing :: Typed",
|
||||
]
|
||||
dependencies = [
|
||||
"certifi>=2026.2.25",
|
||||
"email-validator>=2.3.0",
|
||||
"httpx>=0.28.1",
|
||||
"httpx-ws>=0.9.0",
|
||||
"pydantic>=2.12.5",
|
||||
]
|
||||
|
||||
@ -18,9 +35,21 @@ build-backend = "hatchling.build"
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"datamodel-code-generator>=0.56.0",
|
||||
"datamodel-code-generator[ruff]>=0.56.0",
|
||||
"mypy>=1.20.0",
|
||||
"pre-commit>=4.6.0",
|
||||
"pydoc-markdown>=4.8.2",
|
||||
"pytest>=9.0.3",
|
||||
"pytest-asyncio>=1.3.0",
|
||||
"respx>=0.23.1",
|
||||
"ruff>=0.15.10",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://wrenn.dev"
|
||||
Repository = "https://github.com/wrennhq/python-sdk"
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
markers = [
|
||||
"integration: integration tests (require live server)",
|
||||
]
|
||||
|
||||
@ -1,2 +1,107 @@
|
||||
def hello() -> str:
|
||||
return "Hello from wrenn!"
|
||||
from wrenn._git import (
|
||||
AsyncGit,
|
||||
FileStatus,
|
||||
Git,
|
||||
GitAuthError,
|
||||
GitBranch,
|
||||
GitCommandError,
|
||||
GitError,
|
||||
GitStatus,
|
||||
)
|
||||
from wrenn.async_capsule import AsyncCapsule
|
||||
from wrenn.capsule import Capsule
|
||||
from wrenn.client import AsyncWrennClient, WrennClient
|
||||
from wrenn.commands import (
|
||||
CommandHandle,
|
||||
CommandResult,
|
||||
ProcessInfo,
|
||||
StreamErrorEvent,
|
||||
StreamEvent,
|
||||
StreamExitEvent,
|
||||
StreamStartEvent,
|
||||
StreamStderrEvent,
|
||||
StreamStdoutEvent,
|
||||
)
|
||||
from wrenn.exceptions import (
|
||||
WrennAgentError,
|
||||
WrennAuthenticationError,
|
||||
WrennConflictError,
|
||||
WrennError,
|
||||
WrennForbiddenError,
|
||||
WrennHostHasCapsulesError,
|
||||
WrennHostUnavailableError,
|
||||
WrennInternalError,
|
||||
WrennNotFoundError,
|
||||
WrennValidationError,
|
||||
)
|
||||
from wrenn.models import FileEntry
|
||||
from wrenn.pty import AsyncPtySession, PtyEvent, PtyEventType, PtySession
|
||||
|
||||
__version__ = "0.1.4"
|
||||
|
||||
__all__ = [
|
||||
"__version__",
|
||||
"AsyncCapsule",
|
||||
"AsyncGit",
|
||||
"AsyncPtySession",
|
||||
"AsyncWrennClient",
|
||||
"Capsule",
|
||||
"CommandHandle",
|
||||
"CommandResult",
|
||||
"FileEntry",
|
||||
"FileStatus",
|
||||
"Git",
|
||||
"GitAuthError",
|
||||
"GitBranch",
|
||||
"GitCommandError",
|
||||
"GitError",
|
||||
"GitStatus",
|
||||
"ProcessInfo",
|
||||
"PtyEvent",
|
||||
"PtyEventType",
|
||||
"PtySession",
|
||||
"Sandbox",
|
||||
"StreamErrorEvent",
|
||||
"StreamEvent",
|
||||
"StreamExitEvent",
|
||||
"StreamStartEvent",
|
||||
"StreamStderrEvent",
|
||||
"StreamStdoutEvent",
|
||||
"WrennAgentError",
|
||||
"WrennAuthenticationError",
|
||||
"WrennClient",
|
||||
"WrennConflictError",
|
||||
"WrennError",
|
||||
"WrennForbiddenError",
|
||||
"WrennHostHasCapsulesError",
|
||||
"WrennHostHasSandboxesError",
|
||||
"WrennHostUnavailableError",
|
||||
"WrennInternalError",
|
||||
"WrennNotFoundError",
|
||||
"WrennValidationError",
|
||||
]
|
||||
|
||||
|
||||
def __getattr__(name: str) -> type:
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
_module = sys.modules[__name__]
|
||||
|
||||
if name == "Sandbox":
|
||||
warnings.warn(
|
||||
"'Sandbox' is deprecated, use 'Capsule' instead",
|
||||
FutureWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
setattr(_module, name, Capsule)
|
||||
return Capsule
|
||||
if name == "WrennHostHasSandboxesError":
|
||||
warnings.warn(
|
||||
"'WrennHostHasSandboxesError' is deprecated, use 'WrennHostHasCapsulesError' instead",
|
||||
FutureWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
setattr(_module, name, WrennHostHasCapsulesError)
|
||||
return WrennHostHasCapsulesError
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
7
src/wrenn/_config.py
Normal file
7
src/wrenn/_config.py
Normal file
@ -0,0 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
DEFAULT_BASE_URL = "https://app.wrenn.dev/api"
|
||||
DEFAULT_PROXY_DOMAIN = "wrenn.dev"
|
||||
ENV_API_KEY = "WRENN_API_KEY"
|
||||
ENV_BASE_URL = "WRENN_BASE_URL"
|
||||
ENV_PROXY_DOMAIN = "WRENN_PROXY_DOMAIN"
|
||||
1475
src/wrenn/_git/__init__.py
Normal file
1475
src/wrenn/_git/__init__.py
Normal file
File diff suppressed because it is too large
Load Diff
104
src/wrenn/_git/_auth.py
Normal file
104
src/wrenn/_git/_auth.py
Normal file
@ -0,0 +1,104 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import shlex
|
||||
from urllib.parse import urlparse, urlunparse
|
||||
|
||||
|
||||
def embed_credentials(url: str, username: str, password: str) -> str:
|
||||
"""Embed HTTP(S) credentials into a git URL.
|
||||
|
||||
Args:
|
||||
url: Git repository URL.
|
||||
username: Username for authentication.
|
||||
password: Password or personal access token.
|
||||
|
||||
Returns:
|
||||
URL with ``username:password@`` embedded in the netloc.
|
||||
|
||||
Raises:
|
||||
ValueError: If the URL scheme is not ``http`` or ``https``.
|
||||
"""
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValueError("Only http(s) URLs support embedded credentials.")
|
||||
netloc = f"{username}:{password}@{parsed.hostname}"
|
||||
if parsed.port:
|
||||
netloc = f"{netloc}:{parsed.port}"
|
||||
return urlunparse(parsed._replace(netloc=netloc))
|
||||
|
||||
|
||||
def strip_credentials(url: str) -> str:
|
||||
"""Remove embedded credentials from a git URL.
|
||||
|
||||
Args:
|
||||
url: Git repository URL, possibly with credentials.
|
||||
|
||||
Returns:
|
||||
URL with credentials removed. Non-HTTP(S) URLs are returned
|
||||
unchanged.
|
||||
"""
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
return url
|
||||
if not parsed.username and not parsed.password:
|
||||
return url
|
||||
host = parsed.hostname or ""
|
||||
if parsed.port:
|
||||
host = f"{host}:{parsed.port}"
|
||||
return urlunparse(parsed._replace(netloc=host))
|
||||
|
||||
|
||||
def is_auth_error(stderr: str) -> bool:
|
||||
"""Check whether git stderr indicates an authentication failure.
|
||||
|
||||
Args:
|
||||
stderr: Combined stderr output from a git command.
|
||||
|
||||
Returns:
|
||||
``True`` if any known auth-failure pattern is found.
|
||||
"""
|
||||
lower = stderr.lower()
|
||||
patterns = (
|
||||
"authentication failed",
|
||||
"terminal prompts disabled",
|
||||
"could not read username",
|
||||
"invalid username or password",
|
||||
"access denied",
|
||||
"permission denied",
|
||||
"not authorized",
|
||||
)
|
||||
return any(p in lower for p in patterns)
|
||||
|
||||
|
||||
def build_credential_approve_cmd(
|
||||
username: str,
|
||||
password: str,
|
||||
host: str = "github.com",
|
||||
protocol: str = "https",
|
||||
) -> str:
|
||||
"""Build a shell command that pipes credentials into ``git credential approve``.
|
||||
|
||||
Args:
|
||||
username: Git username.
|
||||
password: Password or personal access token.
|
||||
host: Target host. Defaults to ``"github.com"``.
|
||||
protocol: Protocol. Defaults to ``"https"``.
|
||||
|
||||
Returns:
|
||||
A shell command string safe to pass to ``commands.run()``.
|
||||
"""
|
||||
if "\n" in username or "\n" in password:
|
||||
raise ValueError("Credentials must not contain newline characters.")
|
||||
target_host = host.strip() or "github.com"
|
||||
target_protocol = protocol.strip() or "https"
|
||||
credential_input = "\n".join(
|
||||
[
|
||||
f"protocol={target_protocol}",
|
||||
f"host={target_host}",
|
||||
f"username={username}",
|
||||
f"password={password}",
|
||||
"",
|
||||
"",
|
||||
]
|
||||
)
|
||||
return f"printf %s {shlex.quote(credential_input)} | git credential approve"
|
||||
494
src/wrenn/_git/_cmd.py
Normal file
494
src/wrenn/_git/_cmd.py
Normal file
@ -0,0 +1,494 @@
|
||||
"""Pure functions that build git argument lists and parse git output.
|
||||
|
||||
No I/O, no network, no imports from ``wrenn``. Every ``build_*`` function
|
||||
returns a ``list[str]`` suitable for ``shlex.join()``. Every ``parse_*``
|
||||
function takes raw stdout and returns a typed structure.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
# ── Data types ─────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileStatus:
|
||||
"""A single entry from ``git status --porcelain=v1``.
|
||||
|
||||
Attributes:
|
||||
path (str): File path relative to the repository root.
|
||||
index_status (str): Index (staged) status character.
|
||||
work_tree_status (str): Working-tree status character.
|
||||
renamed_from (str | None): Original path when status is a rename.
|
||||
"""
|
||||
|
||||
path: str
|
||||
index_status: str
|
||||
work_tree_status: str
|
||||
renamed_from: str | None = None
|
||||
|
||||
@property
|
||||
def staged(self) -> bool:
|
||||
"""Whether the change is staged in the index."""
|
||||
return self.index_status not in (" ", "?")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
"""Normalized human-readable status label."""
|
||||
return _derive_status(self.index_status, self.work_tree_status)
|
||||
|
||||
|
||||
@dataclass
|
||||
class GitStatus:
|
||||
"""Parsed output of ``git status --porcelain=v1 --branch``.
|
||||
|
||||
Attributes:
|
||||
branch (str | None): Current branch name, or ``None`` if detached.
|
||||
upstream (str | None): Upstream tracking branch.
|
||||
ahead (int): Commits ahead of upstream.
|
||||
behind (int): Commits behind upstream.
|
||||
detached (bool): Whether HEAD is detached.
|
||||
files (list[FileStatus]): Per-file status entries.
|
||||
"""
|
||||
|
||||
branch: str | None = None
|
||||
upstream: str | None = None
|
||||
ahead: int = 0
|
||||
behind: int = 0
|
||||
detached: bool = False
|
||||
files: list[FileStatus] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def is_clean(self) -> bool:
|
||||
"""``True`` when there are no changed or untracked files."""
|
||||
return len(self.files) == 0
|
||||
|
||||
@property
|
||||
def has_staged(self) -> bool:
|
||||
"""``True`` when at least one file has staged changes."""
|
||||
return any(f.staged for f in self.files)
|
||||
|
||||
@property
|
||||
def has_untracked(self) -> bool:
|
||||
"""``True`` when at least one file is untracked."""
|
||||
return any(f.status == "untracked" for f in self.files)
|
||||
|
||||
@property
|
||||
def has_conflicts(self) -> bool:
|
||||
"""``True`` when at least one file has merge conflicts."""
|
||||
return any(f.status == "conflict" for f in self.files)
|
||||
|
||||
|
||||
@dataclass
|
||||
class GitBranch:
|
||||
"""A single branch entry.
|
||||
|
||||
Attributes:
|
||||
name (str): Branch name (short ref).
|
||||
is_current (bool): Whether this is the checked-out branch.
|
||||
"""
|
||||
|
||||
name: str
|
||||
is_current: bool = False
|
||||
|
||||
|
||||
# ── Argument builders ──────────────────────────────────────────────
|
||||
|
||||
|
||||
def build_clone(
|
||||
url: str,
|
||||
dest: str | None = None,
|
||||
*,
|
||||
branch: str | None = None,
|
||||
depth: int | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git clone`` arguments."""
|
||||
args = ["git", "clone"]
|
||||
if branch:
|
||||
args.extend(["--branch", branch, "--single-branch"])
|
||||
if depth is not None:
|
||||
args.extend(["--depth", str(depth)])
|
||||
args.append(url)
|
||||
if dest:
|
||||
args.append(dest)
|
||||
return args
|
||||
|
||||
|
||||
def build_init(
|
||||
path: str = ".",
|
||||
*,
|
||||
bare: bool = False,
|
||||
initial_branch: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git init`` arguments."""
|
||||
args = ["git", "init"]
|
||||
if initial_branch:
|
||||
args.extend(["--initial-branch", initial_branch])
|
||||
if bare:
|
||||
args.append("--bare")
|
||||
args.append(path)
|
||||
return args
|
||||
|
||||
|
||||
def build_add(
|
||||
paths: list[str] | None = None,
|
||||
*,
|
||||
all: bool = False,
|
||||
) -> list[str]:
|
||||
"""Build ``git add`` arguments."""
|
||||
args = ["git", "add"]
|
||||
if not paths:
|
||||
args.append("-A" if all else ".")
|
||||
else:
|
||||
args.append("--")
|
||||
args.extend(paths)
|
||||
return args
|
||||
|
||||
|
||||
def build_commit(
|
||||
message: str,
|
||||
*,
|
||||
allow_empty: bool = False,
|
||||
author_name: str | None = None,
|
||||
author_email: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git commit`` arguments."""
|
||||
args = ["git"]
|
||||
if author_name:
|
||||
args.extend(["-c", f"user.name={author_name}"])
|
||||
if author_email:
|
||||
args.extend(["-c", f"user.email={author_email}"])
|
||||
args.extend(["commit", "-m", message])
|
||||
if allow_empty:
|
||||
args.append("--allow-empty")
|
||||
return args
|
||||
|
||||
|
||||
def build_push(
|
||||
remote: str = "origin",
|
||||
branch: str | None = None,
|
||||
*,
|
||||
force: bool = False,
|
||||
set_upstream: bool = False,
|
||||
) -> list[str]:
|
||||
"""Build ``git push`` arguments."""
|
||||
args = ["git", "push"]
|
||||
if force:
|
||||
args.append("--force")
|
||||
if set_upstream:
|
||||
args.append("--set-upstream")
|
||||
args.append(remote)
|
||||
if branch:
|
||||
args.append(branch)
|
||||
return args
|
||||
|
||||
|
||||
def build_pull(
|
||||
remote: str = "origin",
|
||||
branch: str | None = None,
|
||||
*,
|
||||
rebase: bool = False,
|
||||
ff_only: bool = False,
|
||||
) -> list[str]:
|
||||
"""Build ``git pull`` arguments."""
|
||||
args = ["git", "pull"]
|
||||
if rebase:
|
||||
args.append("--rebase")
|
||||
if ff_only:
|
||||
args.append("--ff-only")
|
||||
args.append(remote)
|
||||
if branch:
|
||||
args.append(branch)
|
||||
return args
|
||||
|
||||
|
||||
def build_status() -> list[str]:
|
||||
"""Build ``git status`` arguments for porcelain parsing."""
|
||||
return ["git", "status", "--porcelain=v1", "--branch"]
|
||||
|
||||
|
||||
def build_branches() -> list[str]:
|
||||
"""Build ``git branch`` arguments for structured parsing."""
|
||||
return ["git", "branch", "--format=%(refname:short)\t%(HEAD)"]
|
||||
|
||||
|
||||
def build_create_branch(
|
||||
name: str,
|
||||
*,
|
||||
start_point: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git checkout -b`` arguments."""
|
||||
args = ["git", "checkout", "-b", name]
|
||||
if start_point:
|
||||
args.append(start_point)
|
||||
return args
|
||||
|
||||
|
||||
def build_checkout(name: str) -> list[str]:
|
||||
"""Build ``git checkout`` arguments."""
|
||||
return ["git", "checkout", name]
|
||||
|
||||
|
||||
def build_delete_branch(
|
||||
name: str,
|
||||
*,
|
||||
force: bool = False,
|
||||
) -> list[str]:
|
||||
"""Build ``git branch -d/-D`` arguments."""
|
||||
return ["git", "branch", "-D" if force else "-d", name]
|
||||
|
||||
|
||||
def build_remote_add(name: str, url: str, *, fetch: bool = False) -> list[str]:
|
||||
"""Build ``git remote add`` arguments."""
|
||||
args = ["git", "remote", "add"]
|
||||
if fetch:
|
||||
args.append("-f")
|
||||
args.extend([name, url])
|
||||
return args
|
||||
|
||||
|
||||
def build_remote_get_url(name: str = "origin") -> list[str]:
|
||||
"""Build ``git remote get-url`` arguments."""
|
||||
return ["git", "remote", "get-url", name]
|
||||
|
||||
|
||||
def build_remote_set_url(name: str, url: str) -> list[str]:
|
||||
"""Build ``git remote set-url`` arguments."""
|
||||
return ["git", "remote", "set-url", name, url]
|
||||
|
||||
|
||||
def build_reset(
|
||||
*,
|
||||
mode: str | None = None,
|
||||
ref: str | None = None,
|
||||
paths: list[str] | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git reset`` arguments.
|
||||
|
||||
Args:
|
||||
mode: Reset mode (``soft``, ``mixed``, ``hard``, ``merge``, ``keep``).
|
||||
ref: Commit, branch, or ref to reset to.
|
||||
paths: Paths to reset (mutually exclusive with ``mode``).
|
||||
"""
|
||||
_ALLOWED_MODES = {"soft", "mixed", "hard", "merge", "keep"}
|
||||
if mode and mode not in _ALLOWED_MODES:
|
||||
raise ValueError(
|
||||
f"Reset mode must be one of {', '.join(sorted(_ALLOWED_MODES))}."
|
||||
)
|
||||
args = ["git", "reset"]
|
||||
if mode:
|
||||
args.append(f"--{mode}")
|
||||
if ref:
|
||||
args.append(ref)
|
||||
if paths:
|
||||
args.append("--")
|
||||
args.extend(paths)
|
||||
return args
|
||||
|
||||
|
||||
def build_restore(
|
||||
paths: list[str],
|
||||
*,
|
||||
staged: bool = False,
|
||||
worktree: bool = False,
|
||||
source: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git restore`` arguments.
|
||||
|
||||
Args:
|
||||
paths: Paths to restore.
|
||||
staged: Restore the index (unstage).
|
||||
worktree: Restore working-tree files.
|
||||
source: Commit or ref to restore from.
|
||||
"""
|
||||
if not paths:
|
||||
raise ValueError("At least one path is required.")
|
||||
if not staged and not worktree:
|
||||
worktree = True
|
||||
args = ["git", "restore"]
|
||||
if worktree:
|
||||
args.append("--worktree")
|
||||
if staged:
|
||||
args.append("--staged")
|
||||
if source:
|
||||
args.extend(["--source", source])
|
||||
args.append("--")
|
||||
args.extend(paths)
|
||||
return args
|
||||
|
||||
|
||||
def build_config_set(
|
||||
key: str,
|
||||
value: str,
|
||||
*,
|
||||
scope: str = "local",
|
||||
repo_path: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git config`` set arguments."""
|
||||
scope_flag = _resolve_scope_flag(scope)
|
||||
args = ["git"]
|
||||
if scope == "local" and repo_path:
|
||||
args.extend(["-C", repo_path])
|
||||
args.extend(["config", scope_flag, key, value])
|
||||
return args
|
||||
|
||||
|
||||
def build_config_get(
|
||||
key: str,
|
||||
*,
|
||||
scope: str = "local",
|
||||
repo_path: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``git config --get`` arguments."""
|
||||
scope_flag = _resolve_scope_flag(scope)
|
||||
args = ["git"]
|
||||
if scope == "local" and repo_path:
|
||||
args.extend(["-C", repo_path])
|
||||
args.extend(["config", scope_flag, "--get", key])
|
||||
return args
|
||||
|
||||
|
||||
# ── Parsers ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def parse_status(stdout: str) -> GitStatus:
|
||||
"""Parse ``git status --porcelain=v1 --branch`` output.
|
||||
|
||||
Args:
|
||||
stdout: Raw stdout from the git status command.
|
||||
|
||||
Returns:
|
||||
Parsed :class:`GitStatus`.
|
||||
"""
|
||||
lines = [line for line in stdout.split("\n") if line.rstrip()]
|
||||
if not lines:
|
||||
return GitStatus()
|
||||
|
||||
status = GitStatus()
|
||||
|
||||
branch_line = lines[0]
|
||||
if branch_line.startswith("## "):
|
||||
_parse_branch_line(branch_line[3:], status)
|
||||
|
||||
for line in lines[1:]:
|
||||
if line.startswith("?? "):
|
||||
status.files.append(
|
||||
FileStatus(
|
||||
path=line[3:],
|
||||
index_status="?",
|
||||
work_tree_status="?",
|
||||
)
|
||||
)
|
||||
continue
|
||||
|
||||
if len(line) < 4:
|
||||
continue
|
||||
|
||||
idx = line[0]
|
||||
wt = line[1]
|
||||
path = line[3:]
|
||||
renamed_from = None
|
||||
if " -> " in path:
|
||||
renamed_from, path = path.split(" -> ", 1)
|
||||
|
||||
status.files.append(
|
||||
FileStatus(
|
||||
path=path,
|
||||
index_status=idx,
|
||||
work_tree_status=wt,
|
||||
renamed_from=renamed_from,
|
||||
)
|
||||
)
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def parse_branches(stdout: str) -> list[GitBranch]:
|
||||
"""Parse ``git branch --format=%(refname:short)\\t%(HEAD)`` output.
|
||||
|
||||
Args:
|
||||
stdout: Raw stdout from the git branch command.
|
||||
|
||||
Returns:
|
||||
List of :class:`GitBranch`.
|
||||
"""
|
||||
branches: list[GitBranch] = []
|
||||
for line in stdout.split("\n"):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
parts = line.split("\t")
|
||||
name = parts[0]
|
||||
is_current = len(parts) > 1 and parts[1] == "*"
|
||||
branches.append(GitBranch(name=name, is_current=is_current))
|
||||
return branches
|
||||
|
||||
|
||||
# ── Internal helpers ───────────────────────────────────────────────
|
||||
|
||||
|
||||
def _resolve_scope_flag(scope: str) -> str:
|
||||
"""Convert a scope name to a git config flag."""
|
||||
scope = scope.strip().lower()
|
||||
if scope == "local":
|
||||
return "--local"
|
||||
if scope == "global":
|
||||
return "--global"
|
||||
if scope == "system":
|
||||
return "--system"
|
||||
raise ValueError("Git config scope must be one of: local, global, system.")
|
||||
|
||||
|
||||
def _parse_branch_line(info: str, status: GitStatus) -> None:
|
||||
"""Parse the ``## branch...upstream [ahead N, behind M]`` header."""
|
||||
ahead_start = info.find(" [")
|
||||
branch_part = info if ahead_start == -1 else info[:ahead_start]
|
||||
ahead_part = None if ahead_start == -1 else info[ahead_start + 2 : -1]
|
||||
|
||||
if branch_part.startswith("HEAD (detached at "):
|
||||
status.detached = True
|
||||
status.branch = branch_part[18:].rstrip(")")
|
||||
elif "detached" in branch_part or branch_part.startswith("HEAD"):
|
||||
status.detached = True
|
||||
elif "..." in branch_part:
|
||||
local, remote = branch_part.split("...", 1)
|
||||
status.branch = local or None
|
||||
status.upstream = remote or None
|
||||
else:
|
||||
name = branch_part.replace("No commits yet on ", "").replace(
|
||||
"Initial commit on ", ""
|
||||
)
|
||||
status.branch = name or None
|
||||
|
||||
if ahead_part:
|
||||
m = re.search(r"ahead (\d+)", ahead_part)
|
||||
if m:
|
||||
status.ahead = int(m.group(1))
|
||||
m = re.search(r"behind (\d+)", ahead_part)
|
||||
if m:
|
||||
status.behind = int(m.group(1))
|
||||
|
||||
|
||||
def _derive_status(index_status: str, work_tree_status: str) -> str:
|
||||
"""Derive a normalized status label from porcelain XY characters."""
|
||||
chars = {index_status, work_tree_status}
|
||||
if "U" in chars:
|
||||
return "conflict"
|
||||
if "R" in chars:
|
||||
return "renamed"
|
||||
if "C" in chars:
|
||||
return "copied"
|
||||
if "D" in chars:
|
||||
return "deleted"
|
||||
if "A" in chars:
|
||||
return "added"
|
||||
if "M" in chars:
|
||||
return "modified"
|
||||
if "T" in chars:
|
||||
return "typechange"
|
||||
if "?" in chars:
|
||||
return "untracked"
|
||||
return "unknown"
|
||||
28
src/wrenn/_git/exceptions.py
Normal file
28
src/wrenn/_git/exceptions.py
Normal file
@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class GitError(Exception):
|
||||
"""Base exception for all git operations inside a capsule.
|
||||
|
||||
Not a subclass of :class:`WrennError` because git errors originate
|
||||
from a process exit code, not an HTTP response.
|
||||
|
||||
Attributes:
|
||||
message (str): Human-readable error description.
|
||||
stderr (str): Raw stderr output from the git process.
|
||||
exit_code (int): Process exit code.
|
||||
"""
|
||||
|
||||
def __init__(self, message: str, *, stderr: str = "", exit_code: int = -1) -> None:
|
||||
self.message = message
|
||||
self.stderr = stderr
|
||||
self.exit_code = exit_code
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
class GitCommandError(GitError):
|
||||
"""A git command exited with a non-zero exit code."""
|
||||
|
||||
|
||||
class GitAuthError(GitError):
|
||||
"""Authentication failed when communicating with a remote."""
|
||||
491
src/wrenn/async_capsule.py
Normal file
491
src/wrenn/async_capsule.py
Normal file
@ -0,0 +1,491 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import builtins
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import httpx_ws
|
||||
|
||||
from wrenn._git import AsyncGit
|
||||
from wrenn.capsule import (
|
||||
_DEFAULT_WAIT_TIMEOUT,
|
||||
_DESTROY_INTERVAL,
|
||||
_FAIL_STATUSES,
|
||||
_PAUSE_INTERVAL,
|
||||
_RESUME_INTERVAL,
|
||||
_START_INTERVAL,
|
||||
_DualMethod,
|
||||
_build_http_proxy_url,
|
||||
)
|
||||
from wrenn.client import AsyncWrennClient
|
||||
from wrenn.commands import AsyncCommands
|
||||
from wrenn.exceptions import WrennNotFoundError
|
||||
from wrenn.files import AsyncFiles
|
||||
from wrenn.models import Capsule as CapsuleModel
|
||||
from wrenn.models import Status, Template
|
||||
from wrenn.pty import AsyncPtySession
|
||||
|
||||
|
||||
async def _apoll_until(
|
||||
fetch,
|
||||
targets: set[Status],
|
||||
interval: float,
|
||||
timeout: float = _DEFAULT_WAIT_TIMEOUT,
|
||||
fail_on: set[Status] | None = None,
|
||||
) -> CapsuleModel:
|
||||
fail = fail_on if fail_on is not None else _FAIL_STATUSES
|
||||
treat_missing_as_target = Status.missing in targets
|
||||
deadline = time.monotonic() + timeout
|
||||
last: CapsuleModel | None = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
last = await fetch()
|
||||
except WrennNotFoundError:
|
||||
if treat_missing_as_target:
|
||||
return CapsuleModel(status=Status.missing)
|
||||
raise
|
||||
if last.status in targets:
|
||||
return last
|
||||
if last.status is not None and last.status in fail:
|
||||
raise RuntimeError(f"Capsule entered {last.status} state while waiting")
|
||||
await asyncio.sleep(interval)
|
||||
raise TimeoutError(
|
||||
f"Capsule did not reach {targets} within {timeout}s "
|
||||
f"(last status: {last.status if last else 'unknown'})"
|
||||
)
|
||||
|
||||
|
||||
class AsyncCapsule:
|
||||
"""Async Wrenn capsule with e2b-compatible interface.
|
||||
|
||||
Create via classmethod::
|
||||
|
||||
capsule = await AsyncCapsule.create(template="minimal")
|
||||
|
||||
Use as async context manager::
|
||||
|
||||
async with await AsyncCapsule.create() as capsule:
|
||||
await capsule.commands.run("echo hello")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
_capsule_id: str,
|
||||
_client: AsyncWrennClient,
|
||||
_info: CapsuleModel | None = None,
|
||||
) -> None:
|
||||
self._id = _capsule_id
|
||||
self._client = _client
|
||||
self._info = _info
|
||||
|
||||
self.commands = AsyncCommands(_capsule_id, _client.http)
|
||||
self.files = AsyncFiles(_capsule_id, _client.http)
|
||||
self.git = AsyncGit(_capsule_id, _client.http)
|
||||
|
||||
# ── Properties ──────────────────────────────────────────────
|
||||
|
||||
@property
|
||||
def capsule_id(self) -> str:
|
||||
"""The capsule's unique identifier.
|
||||
|
||||
Returns:
|
||||
str: Capsule ID assigned by the Wrenn API.
|
||||
"""
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def info(self) -> CapsuleModel | None:
|
||||
"""Cached capsule metadata from the last API call.
|
||||
|
||||
Returns:
|
||||
CapsuleModel | None: The last-fetched capsule model, or ``None``
|
||||
if the capsule was connected without an initial fetch.
|
||||
"""
|
||||
return self._info
|
||||
|
||||
# ── Factory classmethods ────────────────────────────────────
|
||||
|
||||
@classmethod
|
||||
async def create(
|
||||
cls,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout: int | None = None,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> AsyncCapsule:
|
||||
"""Create a new capsule.
|
||||
|
||||
Args:
|
||||
template (str | None): Template name to boot from.
|
||||
vcpus (int | None): Number of virtual CPUs.
|
||||
memory_mb (int | None): Memory in MiB.
|
||||
timeout (int | None): Inactivity TTL in seconds before auto-pause.
|
||||
wait (bool): Await until the capsule reaches ``running`` status.
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
AsyncCapsule: A new capsule instance.
|
||||
"""
|
||||
client = AsyncWrennClient(api_key=api_key, base_url=base_url)
|
||||
try:
|
||||
info = await client.capsules.create(
|
||||
template=template,
|
||||
vcpus=vcpus,
|
||||
memory_mb=memory_mb,
|
||||
timeout_sec=timeout,
|
||||
)
|
||||
if info.id is None:
|
||||
raise RuntimeError("API returned a capsule without an ID")
|
||||
capsule = cls(
|
||||
_capsule_id=info.id,
|
||||
_client=client,
|
||||
_info=info,
|
||||
)
|
||||
if wait:
|
||||
await capsule.wait_ready()
|
||||
return capsule
|
||||
except BaseException:
|
||||
await client.aclose()
|
||||
raise
|
||||
|
||||
@classmethod
|
||||
async def connect(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> AsyncCapsule:
|
||||
"""Connect to an existing capsule, resuming it if paused.
|
||||
|
||||
Args:
|
||||
capsule_id (str): ID of the capsule to connect to.
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
AsyncCapsule: A capsule instance bound to the existing capsule.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
client = AsyncWrennClient(api_key=api_key, base_url=base_url)
|
||||
try:
|
||||
info = await client.capsules.get(capsule_id)
|
||||
|
||||
capsule = cls(
|
||||
_capsule_id=capsule_id,
|
||||
_client=client,
|
||||
_info=info,
|
||||
)
|
||||
|
||||
if info.status == Status.pausing:
|
||||
info = await capsule._wait_for_status({Status.paused}, _PAUSE_INTERVAL)
|
||||
if info.status == Status.paused:
|
||||
await client.capsules.resume(capsule_id)
|
||||
if info.status != Status.running:
|
||||
await capsule.wait_ready()
|
||||
|
||||
return capsule
|
||||
except BaseException:
|
||||
await client.aclose()
|
||||
raise
|
||||
|
||||
# ── Dual instance/static lifecycle ──────────────────────────
|
||||
|
||||
destroy = _DualMethod("_instance_destroy", "_static_destroy")
|
||||
pause = _DualMethod("_instance_pause", "_static_pause")
|
||||
resume = _DualMethod("_instance_resume", "_static_resume")
|
||||
get_info = _DualMethod("_instance_get_info", "_static_get_info")
|
||||
|
||||
async def _instance_destroy(self, wait: bool = False) -> None:
|
||||
await self._client.capsules.destroy(self._id)
|
||||
if wait:
|
||||
await self._wait_for_status(
|
||||
{Status.stopped, Status.missing}, _DESTROY_INTERVAL
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def _static_destroy(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> None:
|
||||
async with AsyncWrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
await client.capsules.destroy(capsule_id)
|
||||
if wait:
|
||||
await _apoll_until(
|
||||
lambda: client.capsules.get(capsule_id),
|
||||
{Status.stopped, Status.missing},
|
||||
_DESTROY_INTERVAL,
|
||||
)
|
||||
|
||||
async def _instance_pause(self, wait: bool = False) -> CapsuleModel:
|
||||
self._info = await self._client.capsules.pause(self._id)
|
||||
if wait:
|
||||
self._info = await self._wait_for_status({Status.paused}, _PAUSE_INTERVAL)
|
||||
return self._info
|
||||
|
||||
@classmethod
|
||||
async def _static_pause(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> CapsuleModel:
|
||||
async with AsyncWrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
info = await client.capsules.pause(capsule_id)
|
||||
if wait:
|
||||
info = await _apoll_until(
|
||||
lambda: client.capsules.get(capsule_id),
|
||||
{Status.paused},
|
||||
_PAUSE_INTERVAL,
|
||||
)
|
||||
return info
|
||||
|
||||
async def _instance_resume(self, wait: bool = False) -> CapsuleModel:
|
||||
self._info = await self._client.capsules.resume(self._id)
|
||||
if wait:
|
||||
self._info = await self._wait_for_status({Status.running}, _RESUME_INTERVAL)
|
||||
return self._info
|
||||
|
||||
@classmethod
|
||||
async def _static_resume(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> CapsuleModel:
|
||||
async with AsyncWrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
info = await client.capsules.resume(capsule_id)
|
||||
if wait:
|
||||
info = await _apoll_until(
|
||||
lambda: client.capsules.get(capsule_id),
|
||||
{Status.running},
|
||||
_RESUME_INTERVAL,
|
||||
)
|
||||
return info
|
||||
|
||||
async def _instance_get_info(self) -> CapsuleModel:
|
||||
self._info = await self._client.capsules.get(self._id)
|
||||
return self._info
|
||||
|
||||
@classmethod
|
||||
async def _static_get_info(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> CapsuleModel:
|
||||
async with AsyncWrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
return await client.capsules.get(capsule_id)
|
||||
|
||||
# ── Instance-only methods ───────────────────────────────────
|
||||
|
||||
async def ping(self) -> None:
|
||||
"""Reset the capsule inactivity timer.
|
||||
|
||||
Call this to prevent the capsule from being auto-paused when the
|
||||
inactivity TTL is set.
|
||||
"""
|
||||
await self._client.capsules.ping(self._id)
|
||||
|
||||
async def _wait_for_status(
|
||||
self,
|
||||
targets: set[Status],
|
||||
interval: float,
|
||||
timeout: float = _DEFAULT_WAIT_TIMEOUT,
|
||||
) -> CapsuleModel:
|
||||
info = await _apoll_until(
|
||||
lambda: self._client.capsules.get(self._id),
|
||||
targets,
|
||||
interval,
|
||||
timeout,
|
||||
fail_on={Status.error, Status.stopped, Status.missing} - targets,
|
||||
)
|
||||
self._info = info
|
||||
return info
|
||||
|
||||
async def wait_ready(self, timeout: float = _DEFAULT_WAIT_TIMEOUT) -> None:
|
||||
"""Await until capsule status is ``running``.
|
||||
|
||||
Raises:
|
||||
TimeoutError: If capsule does not reach ``running`` within ``timeout``.
|
||||
RuntimeError: If capsule enters error/stopped/missing while waiting.
|
||||
"""
|
||||
await self._wait_for_status({Status.running}, _START_INTERVAL, timeout)
|
||||
|
||||
async def is_running(self) -> bool:
|
||||
"""Check whether the capsule is currently running.
|
||||
|
||||
Makes a live API call to fetch current status.
|
||||
|
||||
Returns:
|
||||
bool: ``True`` if the capsule status is ``running``.
|
||||
"""
|
||||
info = await self._instance_get_info()
|
||||
return info.status == Status.running
|
||||
|
||||
# ── Static list ─────────────────────────────────────────────
|
||||
|
||||
@classmethod
|
||||
async def list(
|
||||
cls,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> list[CapsuleModel]:
|
||||
"""List all capsules belonging to the team.
|
||||
|
||||
Args:
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
list[CapsuleModel]: All capsules for the authenticated team.
|
||||
"""
|
||||
async with AsyncWrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
return await client.capsules.list()
|
||||
|
||||
# ── PTY ─────────────────────────────────────────────────────
|
||||
|
||||
@asynccontextmanager
|
||||
async def pty(
|
||||
self,
|
||||
cmd: str = "/bin/bash",
|
||||
args: builtins.list[str] | None = None,
|
||||
cols: int = 80,
|
||||
rows: int = 24,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
) -> AsyncIterator[AsyncPtySession]:
|
||||
"""Open an async interactive PTY session backed by a WebSocket.
|
||||
|
||||
Use as an async context manager and async iterate over
|
||||
:class:`PtyEvent` objects::
|
||||
|
||||
async with capsule.pty() as term:
|
||||
await term.write(b"echo hello\\n")
|
||||
async for event in term:
|
||||
if event.type == "output":
|
||||
print(event.data.decode())
|
||||
|
||||
Args:
|
||||
cmd (str): Command to run inside the PTY. Defaults to
|
||||
``"/bin/bash"``.
|
||||
args (list[str] | None): Additional arguments for ``cmd``.
|
||||
cols (int): Initial terminal column count. Defaults to ``80``.
|
||||
rows (int): Initial terminal row count. Defaults to ``24``.
|
||||
envs (dict[str, str] | None): Additional environment variables
|
||||
to inject into the process.
|
||||
cwd (str | None): Working directory for the process.
|
||||
|
||||
Yields:
|
||||
AsyncPtySession: An interactive async PTY session.
|
||||
"""
|
||||
async with httpx_ws.aconnect_ws(
|
||||
f"/v1/capsules/{self._id}/pty", client=self._client.http
|
||||
) as ws: # type: httpx_ws.AsyncWebSocketSession
|
||||
session = AsyncPtySession(ws, self._id)
|
||||
await session._send_start(
|
||||
cmd=cmd, args=args, cols=cols, rows=rows, envs=envs, cwd=cwd
|
||||
)
|
||||
yield session
|
||||
|
||||
@asynccontextmanager
|
||||
async def pty_connect(self, tag: str) -> AsyncIterator[AsyncPtySession]:
|
||||
"""Reconnect to an existing PTY session by tag.
|
||||
|
||||
Args:
|
||||
tag (str): Session tag returned in the ``started`` PTY event.
|
||||
|
||||
Yields:
|
||||
AsyncPtySession: The reconnected async PTY session.
|
||||
"""
|
||||
async with httpx_ws.aconnect_ws(
|
||||
f"/v1/capsules/{self._id}/pty", client=self._client.http
|
||||
) as ws: # type: httpx_ws.AsyncWebSocketSession
|
||||
session = AsyncPtySession(ws, self._id)
|
||||
await session._send_connect(tag)
|
||||
yield session
|
||||
|
||||
# ── Proxy helpers ───────────────────────────────────────────
|
||||
|
||||
def get_url(self, port: int) -> str:
|
||||
"""Get the HTTP proxy URL for a port exposed inside this capsule.
|
||||
|
||||
Args:
|
||||
port (int): Port number to proxy.
|
||||
|
||||
Returns:
|
||||
str: A ``https://`` (or ``http://``) URL that proxies HTTP
|
||||
requests to the given port inside the capsule. For raw
|
||||
WebSocket access, see the lower-level ``_build_proxy_url``
|
||||
helper or the ``pty()`` API.
|
||||
"""
|
||||
return _build_http_proxy_url(
|
||||
self._client._base_url,
|
||||
self._id,
|
||||
port,
|
||||
self._client._proxy_domain,
|
||||
)
|
||||
|
||||
# ── Snapshots ───────────────────────────────────────────────
|
||||
|
||||
async def create_snapshot(
|
||||
self, name: str | None = None, overwrite: bool = False
|
||||
) -> Template:
|
||||
"""Create a snapshot template from this capsule's current state.
|
||||
|
||||
Args:
|
||||
name (str | None): Name for the snapshot template. Auto-generated
|
||||
if not provided.
|
||||
overwrite (bool): If ``True``, overwrite an existing template with
|
||||
the same name. Defaults to ``False``.
|
||||
|
||||
Returns:
|
||||
Template: The created snapshot template.
|
||||
"""
|
||||
return await self._client.snapshots.create(
|
||||
capsule_id=self._id, name=name, overwrite=overwrite
|
||||
)
|
||||
|
||||
# ── Context manager ─────────────────────────────────────────
|
||||
|
||||
async def __aenter__(self) -> AsyncCapsule:
|
||||
return self
|
||||
|
||||
async def __aexit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: object,
|
||||
) -> None:
|
||||
try:
|
||||
await self._instance_destroy()
|
||||
except Exception as exc:
|
||||
logging.warning("Failed to destroy capsule %s: %s", self._id, exc)
|
||||
try:
|
||||
await self._client.aclose()
|
||||
except Exception:
|
||||
pass
|
||||
592
src/wrenn/capsule.py
Normal file
592
src/wrenn/capsule.py
Normal file
@ -0,0 +1,592 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import builtins
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import httpx_ws
|
||||
|
||||
from wrenn._git import Git
|
||||
from wrenn.client import WrennClient
|
||||
from wrenn.commands import Commands
|
||||
from wrenn.exceptions import WrennNotFoundError
|
||||
from wrenn.files import Files
|
||||
from wrenn.models import Capsule as CapsuleModel
|
||||
from wrenn.models import Status, Template
|
||||
from wrenn.pty import PtySession
|
||||
|
||||
|
||||
def _proxy_url(
|
||||
base_url: str,
|
||||
capsule_id: str | None,
|
||||
port: int,
|
||||
proxy_domain: str | None,
|
||||
*,
|
||||
websocket: bool,
|
||||
) -> str:
|
||||
parsed = httpx.URL(base_url)
|
||||
if proxy_domain:
|
||||
host = proxy_domain
|
||||
else:
|
||||
host = parsed.host
|
||||
if parsed.port:
|
||||
host = f"{host}:{parsed.port}"
|
||||
secure = parsed.scheme not in ("http", "ws")
|
||||
if websocket:
|
||||
scheme = "wss" if secure else "ws"
|
||||
else:
|
||||
scheme = "https" if secure else "http"
|
||||
return f"{scheme}://{port}-{capsule_id}.{host}"
|
||||
|
||||
|
||||
def _build_proxy_url(
|
||||
base_url: str,
|
||||
capsule_id: str | None,
|
||||
port: int,
|
||||
proxy_domain: str | None = None,
|
||||
) -> str:
|
||||
"""Build the WebSocket proxy URL (``ws://`` / ``wss://``)."""
|
||||
return _proxy_url(base_url, capsule_id, port, proxy_domain, websocket=True)
|
||||
|
||||
|
||||
def _build_http_proxy_url(
|
||||
base_url: str,
|
||||
capsule_id: str | None,
|
||||
port: int,
|
||||
proxy_domain: str | None = None,
|
||||
) -> str:
|
||||
"""Build the HTTP proxy URL (``http://`` / ``https://``)."""
|
||||
return _proxy_url(base_url, capsule_id, port, proxy_domain, websocket=False)
|
||||
|
||||
|
||||
_RESUME_INTERVAL = 0.5
|
||||
_DESTROY_INTERVAL = 0.5
|
||||
_PAUSE_INTERVAL = 2.0
|
||||
_START_INTERVAL = 0.5
|
||||
_DEFAULT_WAIT_TIMEOUT = 30.0
|
||||
_FAIL_STATUSES = {Status.error}
|
||||
|
||||
|
||||
def _poll_until(
|
||||
fetch,
|
||||
targets: set[Status],
|
||||
interval: float,
|
||||
timeout: float = _DEFAULT_WAIT_TIMEOUT,
|
||||
fail_on: set[Status] | None = None,
|
||||
) -> CapsuleModel:
|
||||
"""Poll ``fetch()`` until status ∈ ``targets``. Raise on ``fail_on``/timeout."""
|
||||
fail = fail_on if fail_on is not None else _FAIL_STATUSES
|
||||
treat_missing_as_target = Status.missing in targets
|
||||
deadline = time.monotonic() + timeout
|
||||
last: CapsuleModel | None = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
last = fetch()
|
||||
except WrennNotFoundError:
|
||||
if treat_missing_as_target:
|
||||
return CapsuleModel(status=Status.missing)
|
||||
raise
|
||||
if last.status in targets:
|
||||
return last
|
||||
if last.status is not None and last.status in fail:
|
||||
raise RuntimeError(f"Capsule entered {last.status} state while waiting")
|
||||
time.sleep(interval)
|
||||
raise TimeoutError(
|
||||
f"Capsule did not reach {targets} within {timeout}s "
|
||||
f"(last status: {last.status if last else 'unknown'})"
|
||||
)
|
||||
|
||||
|
||||
class _DualMethod:
|
||||
"""Descriptor that dispatches to instance method or classmethod depending on call site."""
|
||||
|
||||
def __init__(self, instance_fn_name: str, static_fn_name: str) -> None:
|
||||
self._ifn = instance_fn_name
|
||||
self._sfn = static_fn_name
|
||||
|
||||
def __set_name__(self, owner: type, name: str) -> None:
|
||||
self._name = name
|
||||
|
||||
def __get__(self, obj: Any, cls: type) -> Any:
|
||||
if obj is None:
|
||||
return getattr(cls, self._sfn)
|
||||
return getattr(obj, self._ifn)
|
||||
|
||||
|
||||
class Capsule:
|
||||
"""A Wrenn capsule (sandbox) with e2b-compatible interface.
|
||||
|
||||
Create directly::
|
||||
|
||||
capsule = Capsule(api_key="wrn_...")
|
||||
capsule = Capsule(template="minimal") # reads WRENN_API_KEY env
|
||||
|
||||
Or via classmethod::
|
||||
|
||||
capsule = Capsule.create(template="minimal")
|
||||
|
||||
Use as context manager for automatic cleanup::
|
||||
|
||||
with Capsule() as capsule:
|
||||
capsule.commands.run("echo hello")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout: int | None = None,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
# Private: used by classmethods to skip creation
|
||||
_capsule_id: str | None = None,
|
||||
_client: WrennClient | None = None,
|
||||
_info: CapsuleModel | None = None,
|
||||
) -> None:
|
||||
"""Create and start a new capsule.
|
||||
|
||||
Args:
|
||||
template (str | None): Template name to boot from. Defaults to
|
||||
the server-side default (``"minimal"``).
|
||||
vcpus (int | None): Number of virtual CPUs. Defaults to the
|
||||
server-side default.
|
||||
memory_mb (int | None): Memory in MiB. Defaults to the
|
||||
server-side default.
|
||||
timeout (int | None): Inactivity TTL in seconds before the capsule
|
||||
is auto-paused. ``0`` disables auto-pause.
|
||||
wait (bool): If ``True``, block until the capsule status is
|
||||
``running`` before returning.
|
||||
api_key (str | None): Wrenn API key (``wrn_...``). Falls back to
|
||||
the ``WRENN_API_KEY`` environment variable.
|
||||
base_url (str | None): Wrenn API base URL. Falls back to
|
||||
``WRENN_BASE_URL`` or the default production endpoint.
|
||||
"""
|
||||
if _capsule_id is not None:
|
||||
assert _client is not None
|
||||
self._id: str = _capsule_id
|
||||
self._client = _client
|
||||
self._info = _info
|
||||
else:
|
||||
self._client = WrennClient(api_key=api_key, base_url=base_url)
|
||||
try:
|
||||
self._info = self._client.capsules.create(
|
||||
template=template,
|
||||
vcpus=vcpus,
|
||||
memory_mb=memory_mb,
|
||||
timeout_sec=timeout,
|
||||
)
|
||||
if self._info.id is None:
|
||||
raise RuntimeError("API returned a capsule without an ID")
|
||||
self._id = self._info.id
|
||||
except Exception:
|
||||
self._client.close()
|
||||
raise
|
||||
|
||||
self.commands = Commands(self._id, self._client.http)
|
||||
self.files = Files(self._id, self._client.http)
|
||||
self.git = Git(self._id, self._client.http)
|
||||
|
||||
if wait:
|
||||
self.wait_ready()
|
||||
|
||||
# ── Properties ──────────────────────────────────────────────
|
||||
|
||||
@property
|
||||
def capsule_id(self) -> str:
|
||||
"""The capsule's unique identifier.
|
||||
|
||||
Returns:
|
||||
str: Capsule ID assigned by the Wrenn API.
|
||||
"""
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def info(self) -> CapsuleModel | None:
|
||||
"""Cached capsule metadata from the last API call.
|
||||
|
||||
Returns:
|
||||
CapsuleModel | None: The last-fetched capsule model, or ``None``
|
||||
if the capsule was connected without an initial fetch.
|
||||
"""
|
||||
return self._info
|
||||
|
||||
# ── Factory classmethods ────────────────────────────────────
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout: int | None = None,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> Capsule:
|
||||
"""Create a new capsule.
|
||||
|
||||
Equivalent to calling ``Capsule(...)`` directly.
|
||||
|
||||
Args:
|
||||
template (str | None): Template name to boot from.
|
||||
vcpus (int | None): Number of virtual CPUs.
|
||||
memory_mb (int | None): Memory in MiB.
|
||||
timeout (int | None): Inactivity TTL in seconds before auto-pause.
|
||||
wait (bool): Block until the capsule reaches ``running`` status.
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
Capsule: A new capsule instance.
|
||||
"""
|
||||
return cls(
|
||||
template=template,
|
||||
vcpus=vcpus,
|
||||
memory_mb=memory_mb,
|
||||
timeout=timeout,
|
||||
wait=wait,
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def connect(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> Capsule:
|
||||
"""Connect to an existing capsule, resuming it if paused.
|
||||
|
||||
Args:
|
||||
capsule_id (str): ID of the capsule to connect to.
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
Capsule: A capsule instance bound to the existing capsule.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
client = WrennClient(api_key=api_key, base_url=base_url)
|
||||
info = client.capsules.get(capsule_id)
|
||||
|
||||
capsule = cls(
|
||||
_capsule_id=capsule_id,
|
||||
_client=client,
|
||||
_info=info,
|
||||
)
|
||||
|
||||
if info.status == Status.pausing:
|
||||
info = capsule._wait_for_status({Status.paused}, _PAUSE_INTERVAL)
|
||||
if info.status == Status.paused:
|
||||
client.capsules.resume(capsule_id)
|
||||
if info.status != Status.running:
|
||||
capsule.wait_ready()
|
||||
|
||||
return capsule
|
||||
|
||||
# ── Dual instance/static lifecycle ──────────────────────────
|
||||
|
||||
destroy = _DualMethod("_instance_destroy", "_static_destroy")
|
||||
pause = _DualMethod("_instance_pause", "_static_pause")
|
||||
resume = _DualMethod("_instance_resume", "_static_resume")
|
||||
get_info = _DualMethod("_instance_get_info", "_static_get_info")
|
||||
|
||||
def _instance_destroy(self, wait: bool = False) -> None:
|
||||
"""Destroy this capsule. If ``wait``, poll until stopped/missing."""
|
||||
self._client.capsules.destroy(self._id)
|
||||
if wait:
|
||||
self._wait_for_status({Status.stopped, Status.missing}, _DESTROY_INTERVAL)
|
||||
|
||||
@classmethod
|
||||
def _static_destroy(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> None:
|
||||
"""Destroy a capsule by ID."""
|
||||
with WrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
client.capsules.destroy(capsule_id)
|
||||
if wait:
|
||||
_poll_until(
|
||||
lambda: client.capsules.get(capsule_id),
|
||||
{Status.stopped, Status.missing},
|
||||
_DESTROY_INTERVAL,
|
||||
)
|
||||
|
||||
def _instance_pause(self, wait: bool = False) -> CapsuleModel:
|
||||
"""Pause this capsule. If ``wait``, poll until ``paused``."""
|
||||
self._info = self._client.capsules.pause(self._id)
|
||||
if wait:
|
||||
self._info = self._wait_for_status({Status.paused}, _PAUSE_INTERVAL)
|
||||
return self._info
|
||||
|
||||
@classmethod
|
||||
def _static_pause(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> CapsuleModel:
|
||||
"""Pause a capsule by ID."""
|
||||
with WrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
info = client.capsules.pause(capsule_id)
|
||||
if wait:
|
||||
info = _poll_until(
|
||||
lambda: client.capsules.get(capsule_id),
|
||||
{Status.paused},
|
||||
_PAUSE_INTERVAL,
|
||||
)
|
||||
return info
|
||||
|
||||
def _instance_resume(self, wait: bool = False) -> CapsuleModel:
|
||||
"""Resume this capsule. If ``wait``, poll until ``running``."""
|
||||
self._info = self._client.capsules.resume(self._id)
|
||||
if wait:
|
||||
self._info = self._wait_for_status({Status.running}, _RESUME_INTERVAL)
|
||||
return self._info
|
||||
|
||||
@classmethod
|
||||
def _static_resume(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> CapsuleModel:
|
||||
"""Resume a capsule by ID."""
|
||||
with WrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
info = client.capsules.resume(capsule_id)
|
||||
if wait:
|
||||
info = _poll_until(
|
||||
lambda: client.capsules.get(capsule_id),
|
||||
{Status.running},
|
||||
_RESUME_INTERVAL,
|
||||
)
|
||||
return info
|
||||
|
||||
def _instance_get_info(self) -> CapsuleModel:
|
||||
"""Get current info for this capsule."""
|
||||
self._info = self._client.capsules.get(self._id)
|
||||
return self._info
|
||||
|
||||
@classmethod
|
||||
def _static_get_info(
|
||||
cls,
|
||||
capsule_id: str,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> CapsuleModel:
|
||||
"""Get capsule info by ID."""
|
||||
with WrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
return client.capsules.get(capsule_id)
|
||||
|
||||
# ── Instance-only methods ───────────────────────────────────
|
||||
|
||||
def ping(self) -> None:
|
||||
"""Reset the capsule inactivity timer.
|
||||
|
||||
Call this to prevent the capsule from being auto-paused when the
|
||||
inactivity TTL is set.
|
||||
"""
|
||||
self._client.capsules.ping(self._id)
|
||||
|
||||
def _wait_for_status(
|
||||
self,
|
||||
targets: set[Status],
|
||||
interval: float,
|
||||
timeout: float = _DEFAULT_WAIT_TIMEOUT,
|
||||
) -> CapsuleModel:
|
||||
info = _poll_until(
|
||||
lambda: self._client.capsules.get(self._id),
|
||||
targets,
|
||||
interval,
|
||||
timeout,
|
||||
fail_on={Status.error, Status.stopped, Status.missing} - targets,
|
||||
)
|
||||
self._info = info
|
||||
return info
|
||||
|
||||
def wait_ready(self, timeout: float = _DEFAULT_WAIT_TIMEOUT) -> None:
|
||||
"""Block until capsule status is ``running``.
|
||||
|
||||
Raises:
|
||||
TimeoutError: If capsule does not reach ``running`` within ``timeout``.
|
||||
RuntimeError: If capsule enters error/stopped/missing while waiting.
|
||||
"""
|
||||
self._wait_for_status({Status.running}, _START_INTERVAL, timeout)
|
||||
|
||||
def is_running(self) -> bool:
|
||||
"""Check whether the capsule is currently running.
|
||||
|
||||
Makes a live API call to fetch current status.
|
||||
|
||||
Returns:
|
||||
bool: ``True`` if the capsule status is ``running``.
|
||||
"""
|
||||
info = self._instance_get_info()
|
||||
return info.status == Status.running
|
||||
|
||||
# ── Static list ─────────────────────────────────────────────
|
||||
|
||||
@classmethod
|
||||
def list(
|
||||
cls,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> list[CapsuleModel]:
|
||||
"""List all capsules belonging to the team.
|
||||
|
||||
Args:
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
list[CapsuleModel]: All capsules for the authenticated team.
|
||||
"""
|
||||
with WrennClient(api_key=api_key, base_url=base_url) as client:
|
||||
return client.capsules.list()
|
||||
|
||||
# ── PTY ─────────────────────────────────────────────────────
|
||||
|
||||
@contextmanager
|
||||
def pty(
|
||||
self,
|
||||
cmd: str = "/bin/bash",
|
||||
args: builtins.list[str] | None = None,
|
||||
cols: int = 80,
|
||||
rows: int = 24,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
) -> Iterator[PtySession]:
|
||||
"""Open an interactive PTY session backed by a WebSocket.
|
||||
|
||||
Use as a context manager and iterate over :class:`PtyEvent` objects::
|
||||
|
||||
with capsule.pty() as term:
|
||||
term.write(b"echo hello\\n")
|
||||
for event in term:
|
||||
if event.type == "output":
|
||||
print(event.data.decode())
|
||||
|
||||
Args:
|
||||
cmd (str): Command to run inside the PTY. Defaults to
|
||||
``"/bin/bash"``.
|
||||
args (list[str] | None): Additional arguments for ``cmd``.
|
||||
cols (int): Initial terminal column count. Defaults to ``80``.
|
||||
rows (int): Initial terminal row count. Defaults to ``24``.
|
||||
envs (dict[str, str] | None): Additional environment variables to
|
||||
inject into the process.
|
||||
cwd (str | None): Working directory for the process.
|
||||
|
||||
Yields:
|
||||
PtySession: An interactive PTY session.
|
||||
"""
|
||||
with httpx_ws.connect_ws(
|
||||
f"/v1/capsules/{self._id}/pty", client=self._client.http
|
||||
) as ws: # type: httpx_ws.WebSocketSession
|
||||
session = PtySession(ws, self._id)
|
||||
session._send_start(
|
||||
cmd=cmd, args=args, cols=cols, rows=rows, envs=envs, cwd=cwd
|
||||
)
|
||||
yield session
|
||||
|
||||
@contextmanager
|
||||
def pty_connect(self, tag: str) -> Iterator[PtySession]:
|
||||
"""Reconnect to an existing PTY session by tag.
|
||||
|
||||
Args:
|
||||
tag (str): Session tag returned in the ``started`` PTY event.
|
||||
|
||||
Yields:
|
||||
PtySession: The reconnected PTY session.
|
||||
"""
|
||||
with httpx_ws.connect_ws(
|
||||
f"/v1/capsules/{self._id}/pty", client=self._client.http
|
||||
) as ws: # type: httpx_ws.WebSocketSession
|
||||
session = PtySession(ws, self._id)
|
||||
session._send_connect(tag)
|
||||
yield session
|
||||
|
||||
# ── Proxy helpers ───────────────────────────────────────────
|
||||
|
||||
def get_url(self, port: int) -> str:
|
||||
"""Get the HTTP proxy URL for a port exposed inside this capsule.
|
||||
|
||||
Args:
|
||||
port (int): Port number to proxy.
|
||||
|
||||
Returns:
|
||||
str: A ``https://`` (or ``http://``) URL that proxies HTTP
|
||||
requests to the given port inside the capsule. For raw
|
||||
WebSocket access, see the lower-level ``_build_proxy_url``
|
||||
helper or the ``pty()`` API.
|
||||
"""
|
||||
return _build_http_proxy_url(
|
||||
self._client._base_url,
|
||||
self._id,
|
||||
port,
|
||||
self._client._proxy_domain,
|
||||
)
|
||||
|
||||
# ── Snapshots ───────────────────────────────────────────────
|
||||
|
||||
def create_snapshot(
|
||||
self, name: str | None = None, overwrite: bool = False
|
||||
) -> Template:
|
||||
"""Create a snapshot template from this capsule's current state.
|
||||
|
||||
Args:
|
||||
name (str | None): Name for the snapshot template. Auto-generated
|
||||
if not provided.
|
||||
overwrite (bool): If ``True``, overwrite an existing template with
|
||||
the same name. Defaults to ``False``.
|
||||
|
||||
Returns:
|
||||
Template: The created snapshot template.
|
||||
"""
|
||||
return self._client.snapshots.create(
|
||||
capsule_id=self._id, name=name, overwrite=overwrite
|
||||
)
|
||||
|
||||
# ── Context manager ─────────────────────────────────────────
|
||||
|
||||
def __enter__(self) -> Capsule:
|
||||
return self
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: object,
|
||||
) -> None:
|
||||
try:
|
||||
self._instance_destroy()
|
||||
except Exception as exc:
|
||||
logging.warning("Failed to destroy capsule %s: %s", self._id, exc)
|
||||
try:
|
||||
self._client.close()
|
||||
except Exception:
|
||||
pass
|
||||
595
src/wrenn/client.py
Normal file
595
src/wrenn/client.py
Normal file
@ -0,0 +1,595 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
|
||||
import httpx
|
||||
|
||||
from wrenn._config import (
|
||||
DEFAULT_BASE_URL,
|
||||
DEFAULT_PROXY_DOMAIN,
|
||||
ENV_API_KEY,
|
||||
ENV_BASE_URL,
|
||||
ENV_PROXY_DOMAIN,
|
||||
)
|
||||
from wrenn.exceptions import handle_response
|
||||
|
||||
from wrenn.models import (
|
||||
Template,
|
||||
)
|
||||
from wrenn.models import (
|
||||
Capsule as CapsuleModel,
|
||||
)
|
||||
|
||||
_LONG_TIMEOUT = httpx.Timeout(60.0)
|
||||
_DEFAULT_TIMEOUT = httpx.Timeout(30.0, connect=10.0)
|
||||
|
||||
_RETRY_EXCEPTIONS: tuple[type[BaseException], ...] = (
|
||||
httpx.ReadError,
|
||||
httpx.RemoteProtocolError,
|
||||
httpx.ConnectError,
|
||||
httpx.ReadTimeout,
|
||||
)
|
||||
_RETRY_METHODS = frozenset({"GET", "HEAD", "DELETE", "OPTIONS", "PUT"})
|
||||
_MAX_RETRIES = 3
|
||||
_BACKOFF_BASE = 0.3
|
||||
|
||||
|
||||
def _should_retry(request: httpx.Request, attempt: int) -> bool:
|
||||
return attempt < _MAX_RETRIES - 1 and request.method.upper() in _RETRY_METHODS
|
||||
|
||||
|
||||
def _backoff_delay(attempt: int) -> float:
|
||||
return _BACKOFF_BASE * (2**attempt)
|
||||
|
||||
|
||||
class _RetryingClient(httpx.Client):
|
||||
"""httpx.Client that retries transient TLS/connection errors on
|
||||
idempotent methods (GET/HEAD/DELETE/OPTIONS/PUT). Non-idempotent
|
||||
requests (POST/PATCH) propagate immediately."""
|
||||
|
||||
def send(self, request: httpx.Request, **kwargs): # type: ignore[override]
|
||||
for attempt in range(_MAX_RETRIES):
|
||||
try:
|
||||
return super().send(request, **kwargs)
|
||||
except _RETRY_EXCEPTIONS:
|
||||
if not _should_retry(request, attempt):
|
||||
raise
|
||||
time.sleep(_backoff_delay(attempt))
|
||||
# Unreachable: loop either returns or raises.
|
||||
raise RuntimeError("retry loop exited without result")
|
||||
|
||||
|
||||
class _RetryingAsyncClient(httpx.AsyncClient):
|
||||
"""Async variant of :class:`_RetryingClient`."""
|
||||
|
||||
async def send(self, request: httpx.Request, **kwargs): # type: ignore[override]
|
||||
for attempt in range(_MAX_RETRIES):
|
||||
try:
|
||||
return await super().send(request, **kwargs)
|
||||
except _RETRY_EXCEPTIONS:
|
||||
if not _should_retry(request, attempt):
|
||||
raise
|
||||
await asyncio.sleep(_backoff_delay(attempt))
|
||||
raise RuntimeError("retry loop exited without result")
|
||||
|
||||
|
||||
def _resolve_api_key(api_key: str | None) -> str:
|
||||
resolved = api_key or os.environ.get(ENV_API_KEY)
|
||||
if not resolved:
|
||||
raise ValueError(
|
||||
f"No API key provided. Pass api_key= or set the {ENV_API_KEY} environment variable."
|
||||
)
|
||||
return resolved
|
||||
|
||||
|
||||
def _resolve_timeout(
|
||||
timeout: httpx.Timeout | float | None,
|
||||
) -> httpx.Timeout:
|
||||
if timeout is None:
|
||||
return _DEFAULT_TIMEOUT
|
||||
if isinstance(timeout, httpx.Timeout):
|
||||
return timeout
|
||||
return httpx.Timeout(timeout)
|
||||
|
||||
|
||||
def _resolve_proxy_domain(base_url: str, override: str | None) -> str:
|
||||
"""Resolve proxy host suffix for ``{port}-{capsule_id}.<domain>`` URLs.
|
||||
|
||||
Precedence: explicit ``override`` arg, ``WRENN_PROXY_DOMAIN`` env, then
|
||||
``wrenn.dev`` only when ``base_url`` is the default Wrenn host
|
||||
(``app.wrenn.dev``). Otherwise the ``base_url`` host (with port) is used
|
||||
verbatim — appropriate for local dev or custom deployments.
|
||||
"""
|
||||
resolved = override or os.environ.get(ENV_PROXY_DOMAIN)
|
||||
if resolved:
|
||||
return resolved
|
||||
parsed = httpx.URL(base_url)
|
||||
host = parsed.host
|
||||
if host == "app.wrenn.dev":
|
||||
return DEFAULT_PROXY_DOMAIN
|
||||
if parsed.port:
|
||||
return f"{host}:{parsed.port}"
|
||||
return host
|
||||
|
||||
|
||||
def _build_capsule_create_payload(
|
||||
template: str | None,
|
||||
vcpus: int | None,
|
||||
memory_mb: int | None,
|
||||
timeout_sec: int | None,
|
||||
) -> dict:
|
||||
payload: dict = {}
|
||||
if template is not None:
|
||||
payload["template"] = template
|
||||
if vcpus is not None:
|
||||
payload["vcpus"] = vcpus
|
||||
if memory_mb is not None:
|
||||
payload["memory_mb"] = memory_mb
|
||||
if timeout_sec is not None:
|
||||
payload["timeout_sec"] = timeout_sec
|
||||
return payload
|
||||
|
||||
|
||||
def _build_snapshot_create(
|
||||
capsule_id: str, name: str | None, overwrite: bool
|
||||
) -> tuple[dict, dict]:
|
||||
payload: dict = {"sandbox_id": capsule_id}
|
||||
if name is not None:
|
||||
payload["name"] = name
|
||||
params: dict = {}
|
||||
if overwrite:
|
||||
params["overwrite"] = "true"
|
||||
return payload, params
|
||||
|
||||
|
||||
def _snapshot_list_params(type: str | None) -> dict:
|
||||
params: dict = {}
|
||||
if type is not None:
|
||||
params["type"] = type
|
||||
return params
|
||||
|
||||
|
||||
class CapsulesResource:
|
||||
"""Sync capsule control-plane operations."""
|
||||
|
||||
def __init__(self, http: httpx.Client) -> None:
|
||||
self._http = http
|
||||
|
||||
def create(
|
||||
self,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout_sec: int | None = None,
|
||||
) -> CapsuleModel:
|
||||
"""Create a new capsule.
|
||||
|
||||
Args:
|
||||
template (str | None): Template name to boot from.
|
||||
vcpus (int | None): Number of virtual CPUs.
|
||||
memory_mb (int | None): Memory in MiB.
|
||||
timeout_sec (int | None): Inactivity TTL in seconds before
|
||||
auto-pause. ``0`` disables auto-pause.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: The newly created capsule.
|
||||
"""
|
||||
resp = self._http.post(
|
||||
"/v1/capsules",
|
||||
json=_build_capsule_create_payload(template, vcpus, memory_mb, timeout_sec),
|
||||
)
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
def list(self) -> list[CapsuleModel]:
|
||||
"""List all capsules for the authenticated team.
|
||||
|
||||
Returns:
|
||||
list[CapsuleModel]: All capsules belonging to the team.
|
||||
"""
|
||||
resp = self._http.get("/v1/capsules")
|
||||
return [CapsuleModel.model_validate(item) for item in handle_response(resp)]
|
||||
|
||||
def get(self, id: str) -> CapsuleModel:
|
||||
"""Get a capsule by ID.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: Current state of the capsule.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = self._http.get(f"/v1/capsules/{id}")
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
def destroy(self, id: str) -> None:
|
||||
"""Destroy a capsule permanently.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = self._http.delete(f"/v1/capsules/{id}")
|
||||
handle_response(resp)
|
||||
|
||||
def pause(self, id: str) -> CapsuleModel:
|
||||
"""Pause a running capsule.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: Updated capsule state.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = self._http.post(f"/v1/capsules/{id}/pause")
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
def resume(self, id: str) -> CapsuleModel:
|
||||
"""Resume a paused capsule.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: Updated capsule state.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = self._http.post(f"/v1/capsules/{id}/resume")
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
def ping(self, id: str) -> None:
|
||||
"""Reset the inactivity timer for a capsule.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = self._http.post(f"/v1/capsules/{id}/ping")
|
||||
handle_response(resp)
|
||||
|
||||
|
||||
class AsyncCapsulesResource:
|
||||
"""Async capsule control-plane operations."""
|
||||
|
||||
def __init__(self, http: httpx.AsyncClient) -> None:
|
||||
self._http = http
|
||||
|
||||
async def create(
|
||||
self,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout_sec: int | None = None,
|
||||
) -> CapsuleModel:
|
||||
"""Create a new capsule.
|
||||
|
||||
Args:
|
||||
template (str | None): Template name to boot from.
|
||||
vcpus (int | None): Number of virtual CPUs.
|
||||
memory_mb (int | None): Memory in MiB.
|
||||
timeout_sec (int | None): Inactivity TTL in seconds before
|
||||
auto-pause. ``0`` disables auto-pause.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: The newly created capsule.
|
||||
"""
|
||||
resp = await self._http.post(
|
||||
"/v1/capsules",
|
||||
json=_build_capsule_create_payload(template, vcpus, memory_mb, timeout_sec),
|
||||
)
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
async def list(self) -> list[CapsuleModel]:
|
||||
"""List all capsules for the authenticated team.
|
||||
|
||||
Returns:
|
||||
list[CapsuleModel]: All capsules belonging to the team.
|
||||
"""
|
||||
resp = await self._http.get("/v1/capsules")
|
||||
return [CapsuleModel.model_validate(item) for item in handle_response(resp)]
|
||||
|
||||
async def get(self, id: str) -> CapsuleModel:
|
||||
"""Get a capsule by ID.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: Current state of the capsule.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = await self._http.get(f"/v1/capsules/{id}")
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
async def destroy(self, id: str) -> None:
|
||||
"""Destroy a capsule permanently.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = await self._http.delete(f"/v1/capsules/{id}")
|
||||
handle_response(resp)
|
||||
|
||||
async def pause(self, id: str) -> CapsuleModel:
|
||||
"""Pause a running capsule.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: Updated capsule state.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = await self._http.post(f"/v1/capsules/{id}/pause")
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
async def resume(self, id: str) -> CapsuleModel:
|
||||
"""Resume a paused capsule.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Returns:
|
||||
CapsuleModel: Updated capsule state.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = await self._http.post(f"/v1/capsules/{id}/resume")
|
||||
return CapsuleModel.model_validate(handle_response(resp))
|
||||
|
||||
async def ping(self, id: str) -> None:
|
||||
"""Reset the inactivity timer for a capsule.
|
||||
|
||||
Args:
|
||||
id (str): Capsule ID.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no capsule with the given ID exists.
|
||||
"""
|
||||
resp = await self._http.post(f"/v1/capsules/{id}/ping")
|
||||
handle_response(resp)
|
||||
|
||||
|
||||
class SnapshotsResource:
|
||||
"""Sync snapshot operations."""
|
||||
|
||||
def __init__(self, http: httpx.Client) -> None:
|
||||
self._http = http
|
||||
|
||||
def create(
|
||||
self,
|
||||
capsule_id: str,
|
||||
name: str | None = None,
|
||||
overwrite: bool = False,
|
||||
) -> Template:
|
||||
"""Create a snapshot template from a running capsule.
|
||||
|
||||
Args:
|
||||
capsule_id (str): ID of the capsule to snapshot.
|
||||
name (str | None): Name for the snapshot template. Auto-generated
|
||||
if not provided.
|
||||
overwrite (bool): If ``True``, overwrite an existing template with
|
||||
the same name. Defaults to ``False``.
|
||||
|
||||
Returns:
|
||||
Template: The created snapshot template.
|
||||
"""
|
||||
payload, params = _build_snapshot_create(capsule_id, name, overwrite)
|
||||
resp = self._http.post(
|
||||
"/v1/snapshots", json=payload, params=params, timeout=_LONG_TIMEOUT
|
||||
)
|
||||
return Template.model_validate(handle_response(resp))
|
||||
|
||||
def list(self, type: str | None = None) -> list[Template]:
|
||||
"""List snapshot templates.
|
||||
|
||||
Args:
|
||||
type (str | None): Filter by template type. Returns all templates
|
||||
if not provided.
|
||||
|
||||
Returns:
|
||||
list[Template]: Matching snapshot templates.
|
||||
"""
|
||||
resp = self._http.get("/v1/snapshots", params=_snapshot_list_params(type))
|
||||
return [Template.model_validate(item) for item in handle_response(resp)]
|
||||
|
||||
def delete(self, name: str) -> None:
|
||||
"""Delete a snapshot template by name.
|
||||
|
||||
Args:
|
||||
name (str): Template name to delete.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no template with the given name exists.
|
||||
"""
|
||||
resp = self._http.delete(f"/v1/snapshots/{name}")
|
||||
handle_response(resp)
|
||||
|
||||
|
||||
class AsyncSnapshotsResource:
|
||||
"""Async snapshot operations."""
|
||||
|
||||
def __init__(self, http: httpx.AsyncClient) -> None:
|
||||
self._http = http
|
||||
|
||||
async def create(
|
||||
self,
|
||||
capsule_id: str,
|
||||
name: str | None = None,
|
||||
overwrite: bool = False,
|
||||
) -> Template:
|
||||
"""Create a snapshot template from a running capsule.
|
||||
|
||||
Args:
|
||||
capsule_id (str): ID of the capsule to snapshot.
|
||||
name (str | None): Name for the snapshot template. Auto-generated
|
||||
if not provided.
|
||||
overwrite (bool): If ``True``, overwrite an existing template with
|
||||
the same name. Defaults to ``False``.
|
||||
|
||||
Returns:
|
||||
Template: The created snapshot template.
|
||||
"""
|
||||
payload, params = _build_snapshot_create(capsule_id, name, overwrite)
|
||||
resp = await self._http.post(
|
||||
"/v1/snapshots", json=payload, params=params, timeout=_LONG_TIMEOUT
|
||||
)
|
||||
return Template.model_validate(handle_response(resp))
|
||||
|
||||
async def list(self, type: str | None = None) -> list[Template]:
|
||||
"""List snapshot templates.
|
||||
|
||||
Args:
|
||||
type (str | None): Filter by template type. Returns all templates
|
||||
if not provided.
|
||||
|
||||
Returns:
|
||||
list[Template]: Matching snapshot templates.
|
||||
"""
|
||||
resp = await self._http.get("/v1/snapshots", params=_snapshot_list_params(type))
|
||||
return [Template.model_validate(item) for item in handle_response(resp)]
|
||||
|
||||
async def delete(self, name: str) -> None:
|
||||
"""Delete a snapshot template by name.
|
||||
|
||||
Args:
|
||||
name (str): Template name to delete.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no template with the given name exists.
|
||||
"""
|
||||
resp = await self._http.delete(f"/v1/snapshots/{name}")
|
||||
handle_response(resp)
|
||||
|
||||
|
||||
class WrennClient:
|
||||
"""Synchronous client for the Wrenn API.
|
||||
|
||||
Authenticates with an API key.
|
||||
|
||||
Args:
|
||||
api_key: API key (``wrn_...``). Falls back to ``WRENN_API_KEY`` env var.
|
||||
base_url: Wrenn API base URL. Falls back to ``WRENN_BASE_URL`` env var.
|
||||
proxy_domain: Host suffix for capsule proxy URLs
|
||||
(``{port}-{capsule_id}.<domain>``). Falls back to
|
||||
``WRENN_PROXY_DOMAIN`` env, then ``wrenn.dev`` when ``base_url``
|
||||
is the default ``app.wrenn.dev`` host, else the ``base_url`` host.
|
||||
timeout: HTTP timeout. Accepts ``httpx.Timeout``, a float (seconds),
|
||||
or ``None`` for the default (30s read/write/pool, 10s connect).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
proxy_domain: str | None = None,
|
||||
timeout: httpx.Timeout | float | None = None,
|
||||
) -> None:
|
||||
self._api_key = _resolve_api_key(api_key)
|
||||
self._base_url = base_url or os.environ.get(ENV_BASE_URL, DEFAULT_BASE_URL)
|
||||
self._proxy_domain = _resolve_proxy_domain(self._base_url, proxy_domain)
|
||||
self._http = _RetryingClient(
|
||||
base_url=self._base_url,
|
||||
headers={"X-API-Key": self._api_key},
|
||||
timeout=_resolve_timeout(timeout),
|
||||
)
|
||||
|
||||
self.capsules = CapsulesResource(self._http)
|
||||
self.snapshots = SnapshotsResource(self._http)
|
||||
|
||||
@property
|
||||
def http(self) -> httpx.Client:
|
||||
"""The underlying httpx.Client (for sub-objects that need direct access)."""
|
||||
return self._http
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close the underlying HTTP connection pool."""
|
||||
self._http.close()
|
||||
|
||||
def __enter__(self) -> WrennClient:
|
||||
return self
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: object,
|
||||
) -> None:
|
||||
self.close()
|
||||
|
||||
|
||||
class AsyncWrennClient:
|
||||
"""Asynchronous client for the Wrenn API.
|
||||
|
||||
Authenticates with an API key.
|
||||
|
||||
Args:
|
||||
api_key: API key (``wrn_...``). Falls back to ``WRENN_API_KEY`` env var.
|
||||
base_url: Wrenn API base URL. Falls back to ``WRENN_BASE_URL`` env var.
|
||||
proxy_domain: Host suffix for capsule proxy URLs
|
||||
(``{port}-{capsule_id}.<domain>``). Falls back to
|
||||
``WRENN_PROXY_DOMAIN`` env, then ``wrenn.dev`` when ``base_url``
|
||||
is the default ``app.wrenn.dev`` host, else the ``base_url`` host.
|
||||
timeout: HTTP timeout. Accepts ``httpx.Timeout``, a float (seconds),
|
||||
or ``None`` for the default (30s read/write/pool, 10s connect).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
proxy_domain: str | None = None,
|
||||
timeout: httpx.Timeout | float | None = None,
|
||||
) -> None:
|
||||
self._api_key = _resolve_api_key(api_key)
|
||||
self._base_url = base_url or os.environ.get(ENV_BASE_URL, DEFAULT_BASE_URL)
|
||||
self._proxy_domain = _resolve_proxy_domain(self._base_url, proxy_domain)
|
||||
self._http = _RetryingAsyncClient(
|
||||
base_url=self._base_url,
|
||||
headers={"X-API-Key": self._api_key},
|
||||
timeout=_resolve_timeout(timeout),
|
||||
)
|
||||
|
||||
self.capsules = AsyncCapsulesResource(self._http)
|
||||
self.snapshots = AsyncSnapshotsResource(self._http)
|
||||
|
||||
@property
|
||||
def http(self) -> httpx.AsyncClient:
|
||||
"""The underlying httpx.AsyncClient."""
|
||||
return self._http
|
||||
|
||||
async def aclose(self) -> None:
|
||||
"""Close the underlying async HTTP connection pool."""
|
||||
await self._http.aclose()
|
||||
|
||||
async def __aenter__(self) -> AsyncWrennClient:
|
||||
return self
|
||||
|
||||
async def __aexit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: object,
|
||||
) -> None:
|
||||
await self.aclose()
|
||||
61
src/wrenn/code_interpreter/__init__.py
Normal file
61
src/wrenn/code_interpreter/__init__.py
Normal file
@ -0,0 +1,61 @@
|
||||
"""Deprecated alias for :mod:`wrenn.code_runner`.
|
||||
|
||||
Importing from ``wrenn.code_interpreter`` emits a ``FutureWarning``.
|
||||
Use ``wrenn.code_runner`` instead.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import warnings as _warnings
|
||||
|
||||
warnings_emitted: bool = False
|
||||
|
||||
|
||||
def _warn_once() -> None:
|
||||
global warnings_emitted
|
||||
if warnings_emitted:
|
||||
return
|
||||
warnings_emitted = True
|
||||
_warnings.warn(
|
||||
"'wrenn.code_interpreter' is deprecated, use 'wrenn.code_runner' instead",
|
||||
FutureWarning,
|
||||
stacklevel=3,
|
||||
)
|
||||
|
||||
|
||||
_warn_once()
|
||||
|
||||
from wrenn.code_runner.async_capsule import AsyncCapsule # noqa: E402
|
||||
from wrenn.code_runner.capsule import Capsule # noqa: E402
|
||||
from wrenn.code_runner.models import ( # noqa: E402
|
||||
Execution,
|
||||
ExecutionError,
|
||||
Logs,
|
||||
Result,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"AsyncCapsule",
|
||||
"Capsule",
|
||||
"Execution",
|
||||
"ExecutionError",
|
||||
"Logs",
|
||||
"Result",
|
||||
"Sandbox",
|
||||
]
|
||||
|
||||
|
||||
def __getattr__(name: str) -> type:
|
||||
import sys
|
||||
|
||||
_module = sys.modules[__name__]
|
||||
|
||||
if name == "Sandbox":
|
||||
_warnings.warn(
|
||||
"'Sandbox' is deprecated, use 'Capsule' instead",
|
||||
FutureWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
setattr(_module, name, Capsule)
|
||||
return Capsule
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
3
src/wrenn/code_interpreter/async_capsule.py
Normal file
3
src/wrenn/code_interpreter/async_capsule.py
Normal file
@ -0,0 +1,3 @@
|
||||
"""Deprecated — use :mod:`wrenn.code_runner.async_capsule`."""
|
||||
|
||||
from wrenn.code_runner.async_capsule import AsyncCapsule # noqa: F401
|
||||
7
src/wrenn/code_interpreter/capsule.py
Normal file
7
src/wrenn/code_interpreter/capsule.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""Deprecated — use :mod:`wrenn.code_runner.capsule`."""
|
||||
|
||||
from wrenn.code_runner.capsule import ( # noqa: F401
|
||||
DEFAULT_KERNEL,
|
||||
DEFAULT_TEMPLATE,
|
||||
Capsule,
|
||||
)
|
||||
8
src/wrenn/code_interpreter/models.py
Normal file
8
src/wrenn/code_interpreter/models.py
Normal file
@ -0,0 +1,8 @@
|
||||
"""Deprecated — use :mod:`wrenn.code_runner.models`."""
|
||||
|
||||
from wrenn.code_runner.models import ( # noqa: F401
|
||||
Execution,
|
||||
ExecutionError,
|
||||
Logs,
|
||||
Result,
|
||||
)
|
||||
51
src/wrenn/code_runner/__init__.py
Normal file
51
src/wrenn/code_runner/__init__.py
Normal file
@ -0,0 +1,51 @@
|
||||
"""Code runner — execute code in persistent Jupyter kernels.
|
||||
|
||||
Uses the ``code-runner-beta`` template and the ``wrenn`` Jupyter
|
||||
kernelspec by default.
|
||||
|
||||
Example::
|
||||
|
||||
from wrenn.code_runner import Capsule
|
||||
|
||||
with Capsule(wait=True) as capsule:
|
||||
result = capsule.run_code("print('hello')")
|
||||
print(result.logs.stdout)
|
||||
"""
|
||||
|
||||
from wrenn.code_runner.async_capsule import AsyncCapsule
|
||||
from wrenn.code_runner.capsule import DEFAULT_KERNEL, DEFAULT_TEMPLATE, Capsule
|
||||
from wrenn.code_runner.models import (
|
||||
Execution,
|
||||
ExecutionError,
|
||||
Logs,
|
||||
Result,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"AsyncCapsule",
|
||||
"Capsule",
|
||||
"DEFAULT_KERNEL",
|
||||
"DEFAULT_TEMPLATE",
|
||||
"Execution",
|
||||
"ExecutionError",
|
||||
"Logs",
|
||||
"Result",
|
||||
"Sandbox",
|
||||
]
|
||||
|
||||
|
||||
def __getattr__(name: str) -> type:
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
_module = sys.modules[__name__]
|
||||
|
||||
if name == "Sandbox":
|
||||
warnings.warn(
|
||||
"'Sandbox' is deprecated, use 'Capsule' instead",
|
||||
FutureWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
setattr(_module, name, Capsule)
|
||||
return Capsule
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
133
src/wrenn/code_runner/_protocol.py
Normal file
133
src/wrenn/code_runner/_protocol.py
Normal file
@ -0,0 +1,133 @@
|
||||
"""Shared Jupyter protocol helpers used by both sync and async capsules.
|
||||
|
||||
Pure functions only — no I/O, no sync/async coupling.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
import uuid
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
from wrenn.capsule import _build_proxy_url
|
||||
from wrenn.code_runner.models import (
|
||||
Execution,
|
||||
ExecutionError,
|
||||
Result,
|
||||
)
|
||||
|
||||
|
||||
def build_execute_request(code: str) -> dict:
|
||||
"""Build a Jupyter ``execute_request`` message envelope.
|
||||
|
||||
Returns:
|
||||
dict: A fully-formed Jupyter shell-channel message ready to be
|
||||
JSON-serialized over the kernel WebSocket. The caller is
|
||||
expected to read ``msg["header"]["msg_id"]`` to correlate
|
||||
responses.
|
||||
"""
|
||||
msg_id = str(uuid.uuid4())
|
||||
return {
|
||||
"header": {
|
||||
"msg_id": msg_id,
|
||||
"msg_type": "execute_request",
|
||||
"username": "wrenn-sdk",
|
||||
"session": str(uuid.uuid4()),
|
||||
"date": time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime()),
|
||||
"version": "5.3",
|
||||
},
|
||||
"parent_header": {},
|
||||
"metadata": {},
|
||||
"content": {
|
||||
"code": code,
|
||||
"silent": False,
|
||||
"store_history": True,
|
||||
"user_expressions": {},
|
||||
"allow_stdin": False,
|
||||
"stop_on_error": True,
|
||||
},
|
||||
"buffers": [],
|
||||
"channel": "shell",
|
||||
}
|
||||
|
||||
|
||||
def pick_kernel_id(kernels: list[dict], kernel_name: str) -> str | None:
|
||||
"""Return the ID of the first kernel matching ``kernel_name``, else ``None``."""
|
||||
for k in kernels:
|
||||
if k.get("name") == kernel_name:
|
||||
return k.get("id")
|
||||
return None
|
||||
|
||||
|
||||
def apply_kernel_message(
|
||||
data: dict,
|
||||
msg_id: str,
|
||||
execution: Execution,
|
||||
emit_error: Callable[[ExecutionError], None],
|
||||
on_result: Callable[[Result], Any] | None,
|
||||
on_stdout: Callable[[str], Any] | None,
|
||||
on_stderr: Callable[[str], Any] | None,
|
||||
) -> bool:
|
||||
"""Apply one Jupyter IOPub message to ``execution``.
|
||||
|
||||
Returns ``True`` when the message marks idle (cell done); the caller
|
||||
should stop reading further messages.
|
||||
"""
|
||||
parent = data.get("parent_header", {}).get("msg_id")
|
||||
if parent != msg_id:
|
||||
return False
|
||||
msg_type = data.get("msg_type") or data.get("header", {}).get("msg_type")
|
||||
content = data.get("content", {})
|
||||
|
||||
if msg_type == "stream":
|
||||
text = content.get("text", "")
|
||||
name = content.get("name", "stdout")
|
||||
if name == "stderr":
|
||||
execution.logs.stderr.append(text)
|
||||
if on_stderr is not None:
|
||||
on_stderr(text)
|
||||
else:
|
||||
execution.logs.stdout.append(text)
|
||||
if on_stdout is not None:
|
||||
on_stdout(text)
|
||||
elif msg_type in ("execute_result", "display_data"):
|
||||
bundle = content.get("data", {})
|
||||
is_main = msg_type == "execute_result"
|
||||
result = Result.from_bundle(bundle, is_main_result=is_main)
|
||||
execution.results.append(result)
|
||||
if is_main:
|
||||
execution.execution_count = content.get("execution_count")
|
||||
if on_result is not None:
|
||||
on_result(result)
|
||||
elif msg_type == "error":
|
||||
emit_error(
|
||||
ExecutionError(
|
||||
name=content.get("ename", ""),
|
||||
value=content.get("evalue", ""),
|
||||
traceback="\n".join(content.get("traceback", [])),
|
||||
)
|
||||
)
|
||||
elif msg_type == "status" and content.get("execution_state") == "idle":
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def validate_language(language: str) -> None:
|
||||
if language != "python":
|
||||
raise ValueError(
|
||||
f"language={language!r} is not supported; only 'python'. "
|
||||
"Use the ``kernel=`` constructor argument to target a "
|
||||
"non-Python kernelspec."
|
||||
)
|
||||
|
||||
|
||||
def build_ws_url(
|
||||
base_url: str,
|
||||
capsule_id: str,
|
||||
kernel_id: str,
|
||||
proxy_domain: str | None = None,
|
||||
) -> str:
|
||||
"""Build the Jupyter kernel WebSocket URL for the given capsule."""
|
||||
proxy = _build_proxy_url(base_url, capsule_id, 8888, proxy_domain)
|
||||
return f"{proxy}/api/kernels/{kernel_id}/channels"
|
||||
334
src/wrenn/code_runner/async_capsule.py
Normal file
334
src/wrenn/code_runner/async_capsule.py
Normal file
@ -0,0 +1,334 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import httpx_ws
|
||||
|
||||
from wrenn.async_capsule import AsyncCapsule as BaseAsyncCapsule
|
||||
from wrenn.capsule import _build_http_proxy_url
|
||||
from wrenn.client import AsyncWrennClient
|
||||
from wrenn.code_runner._protocol import (
|
||||
apply_kernel_message,
|
||||
build_execute_request,
|
||||
build_ws_url,
|
||||
pick_kernel_id,
|
||||
validate_language,
|
||||
)
|
||||
from wrenn.code_runner.capsule import DEFAULT_KERNEL, DEFAULT_TEMPLATE
|
||||
from wrenn.code_runner.models import (
|
||||
Execution,
|
||||
ExecutionError,
|
||||
Result,
|
||||
)
|
||||
|
||||
|
||||
class AsyncCapsule(BaseAsyncCapsule):
|
||||
"""Async code runner capsule with ``run_code`` support.
|
||||
|
||||
Uses ``code-runner-beta`` template and the ``wrenn`` Jupyter
|
||||
kernelspec by default::
|
||||
|
||||
from wrenn.code_runner import AsyncCapsule
|
||||
|
||||
capsule = await AsyncCapsule.create()
|
||||
result = await capsule.run_code("print('hello')")
|
||||
"""
|
||||
|
||||
_kernel_id: str | None
|
||||
_kernel_name: str
|
||||
_proxy_client: httpx.AsyncClient | None
|
||||
_ws: httpx_ws.AsyncWebSocketSession | None
|
||||
_ws_cm: Any
|
||||
|
||||
def __init__(self, *, kernel: str | None = None, **kwargs) -> None:
|
||||
# Set attrs before super().__init__ so __del__ never sees a
|
||||
# half-constructed instance.
|
||||
self._kernel_id = None
|
||||
self._kernel_name = kernel or DEFAULT_KERNEL
|
||||
self._proxy_client = None
|
||||
self._ws = None
|
||||
self._ws_cm = None
|
||||
super().__init__(**kwargs)
|
||||
|
||||
async def _close_ws(self) -> None:
|
||||
cm = getattr(self, "_ws_cm", None)
|
||||
if cm is not None:
|
||||
try:
|
||||
await cm.__aexit__(None, None, None)
|
||||
except Exception:
|
||||
pass
|
||||
self._ws = None
|
||||
self._ws_cm = None
|
||||
|
||||
async def _get_ws(self, kernel_id: str) -> httpx_ws.AsyncWebSocketSession:
|
||||
if self._ws is not None:
|
||||
return self._ws
|
||||
ws_url = build_ws_url(
|
||||
self._client._base_url,
|
||||
self._id,
|
||||
kernel_id,
|
||||
self._client._proxy_domain,
|
||||
)
|
||||
headers = {"X-API-Key": self._client._api_key}
|
||||
cm: Any = httpx_ws.aconnect_ws(ws_url, headers=headers)
|
||||
try:
|
||||
ws = await cm.__aenter__()
|
||||
except BaseException:
|
||||
try:
|
||||
await cm.__aexit__(None, None, None)
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
self._ws_cm = cm
|
||||
self._ws = ws
|
||||
return ws
|
||||
|
||||
async def close(self) -> None:
|
||||
await self._close_ws()
|
||||
proxy = getattr(self, "_proxy_client", None)
|
||||
if proxy is not None:
|
||||
try:
|
||||
await proxy.aclose()
|
||||
except Exception:
|
||||
pass
|
||||
self._proxy_client = None
|
||||
|
||||
def __del__(self) -> None:
|
||||
# Async client cannot be safely closed from __del__; just drop the
|
||||
# reference and let httpx warn if the connection was never closed.
|
||||
# Users should call ``await close()`` or use ``async with``.
|
||||
self._proxy_client = None
|
||||
self._ws = None
|
||||
self._ws_cm = None
|
||||
|
||||
async def _instance_destroy(self, wait: bool = False) -> None:
|
||||
# Release WS + proxy client before destroying the capsule.
|
||||
await self.close()
|
||||
await super()._instance_destroy(wait=wait)
|
||||
|
||||
@classmethod
|
||||
async def create(
|
||||
cls,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout: int | None = None,
|
||||
*,
|
||||
kernel: str | None = None,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> AsyncCapsule:
|
||||
"""Create a new async code runner capsule.
|
||||
|
||||
Args:
|
||||
template (str | None): Template to boot from. Defaults to
|
||||
``"code-runner-beta"``.
|
||||
vcpus (int | None): Number of virtual CPUs.
|
||||
memory_mb (int | None): Memory in MiB.
|
||||
timeout (int | None): Inactivity TTL in seconds before auto-pause.
|
||||
kernel (str | None): Jupyter kernelspec name. Defaults to
|
||||
``"wrenn"``.
|
||||
wait (bool): Await until the capsule reaches ``running`` status.
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
AsyncCapsule: A new async code runner capsule instance.
|
||||
"""
|
||||
client = AsyncWrennClient(api_key=api_key, base_url=base_url)
|
||||
try:
|
||||
info = await client.capsules.create(
|
||||
template=template or DEFAULT_TEMPLATE,
|
||||
vcpus=vcpus,
|
||||
memory_mb=memory_mb,
|
||||
timeout_sec=timeout,
|
||||
)
|
||||
if info.id is None:
|
||||
raise RuntimeError("API returned a capsule without an ID")
|
||||
capsule = cls(
|
||||
kernel=kernel,
|
||||
_capsule_id=info.id,
|
||||
_client=client,
|
||||
_info=info,
|
||||
)
|
||||
if wait:
|
||||
await capsule.wait_ready()
|
||||
return capsule
|
||||
except BaseException:
|
||||
await client.aclose()
|
||||
raise
|
||||
|
||||
def _get_proxy_client(self) -> httpx.AsyncClient:
|
||||
if self._proxy_client is None:
|
||||
url = _build_http_proxy_url(
|
||||
self._client._base_url,
|
||||
self._id,
|
||||
8888,
|
||||
self._client._proxy_domain,
|
||||
)
|
||||
self._proxy_client = httpx.AsyncClient(
|
||||
base_url=url,
|
||||
headers={"X-API-Key": self._client._api_key},
|
||||
)
|
||||
return self._proxy_client
|
||||
|
||||
async def _ensure_kernel(self, jupyter_timeout: float = 30) -> str:
|
||||
if self._kernel_id is not None:
|
||||
return self._kernel_id
|
||||
|
||||
client = self._get_proxy_client()
|
||||
deadline = time.monotonic() + jupyter_timeout
|
||||
last_exc: Exception | None = None
|
||||
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
resp = await client.get("/api/kernels")
|
||||
if resp.status_code < 500:
|
||||
resp.raise_for_status()
|
||||
matched = pick_kernel_id(resp.json(), self._kernel_name)
|
||||
if matched is not None:
|
||||
self._kernel_id = matched
|
||||
return matched
|
||||
resp = await client.post(
|
||||
"/api/kernels",
|
||||
json={"name": self._kernel_name},
|
||||
)
|
||||
if resp.status_code < 500:
|
||||
resp.raise_for_status()
|
||||
self._kernel_id = resp.json()["id"]
|
||||
return self._kernel_id
|
||||
last_exc = httpx.HTTPStatusError(
|
||||
f"Jupyter returned {resp.status_code}",
|
||||
request=resp.request,
|
||||
response=resp,
|
||||
)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
if exc.response.status_code < 500:
|
||||
raise
|
||||
last_exc = exc
|
||||
except Exception as exc:
|
||||
last_exc = exc
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
raise TimeoutError(
|
||||
f"Jupyter not available within {jupyter_timeout}s: {last_exc}"
|
||||
)
|
||||
|
||||
async def run_code(
|
||||
self,
|
||||
code: str,
|
||||
language: str = "python",
|
||||
timeout: float = 30,
|
||||
jupyter_timeout: float = 30,
|
||||
on_result: Callable[[Result], Any] | None = None,
|
||||
on_stdout: Callable[[str], Any] | None = None,
|
||||
on_stderr: Callable[[str], Any] | None = None,
|
||||
on_error: Callable[[ExecutionError], Any] | None = None,
|
||||
) -> Execution:
|
||||
"""Execute code in a persistent Jupyter kernel (async).
|
||||
|
||||
Variables, imports, and function definitions survive across calls.
|
||||
|
||||
Args:
|
||||
code: Code string to execute.
|
||||
language: Execution backend language. Currently only ``"python"``
|
||||
is supported; passing anything else raises ``ValueError``.
|
||||
To target a non-Python kernel, set ``kernel=`` on the
|
||||
capsule constructor.
|
||||
timeout: Maximum seconds to wait for execution to complete.
|
||||
jupyter_timeout: Maximum seconds to wait for Jupyter to become
|
||||
available.
|
||||
on_result: Called for each rich output (charts, images, expression
|
||||
values).
|
||||
on_stdout: Called for each stdout chunk.
|
||||
on_stderr: Called for each stderr chunk.
|
||||
on_error: Called when the cell raises an exception.
|
||||
|
||||
Returns:
|
||||
An :class:`Execution` with ``.results``, ``.logs``, ``.error``,
|
||||
and a convenience ``.text`` property.
|
||||
"""
|
||||
validate_language(language)
|
||||
kernel_id = await self._ensure_kernel(jupyter_timeout=jupyter_timeout)
|
||||
|
||||
msg = build_execute_request(code)
|
||||
msg_id = msg["header"]["msg_id"]
|
||||
|
||||
execution = Execution()
|
||||
deadline = time.monotonic() + timeout
|
||||
saw_idle = False
|
||||
|
||||
def _emit_error(err: ExecutionError) -> None:
|
||||
execution.error = err
|
||||
if on_error is not None:
|
||||
on_error(err)
|
||||
|
||||
reconnect_attempts = 1
|
||||
sent = False
|
||||
while True:
|
||||
try:
|
||||
ws = await self._get_ws(kernel_id)
|
||||
if not sent:
|
||||
await ws.send_text(json.dumps(msg))
|
||||
sent = True
|
||||
while True:
|
||||
time_left = deadline - time.monotonic()
|
||||
if time_left <= 0:
|
||||
break
|
||||
data = await asyncio.wait_for(ws.receive_json(), timeout=time_left)
|
||||
if not data:
|
||||
break
|
||||
if apply_kernel_message(
|
||||
data,
|
||||
msg_id,
|
||||
execution,
|
||||
_emit_error,
|
||||
on_result,
|
||||
on_stdout,
|
||||
on_stderr,
|
||||
):
|
||||
saw_idle = True
|
||||
break
|
||||
break
|
||||
except TimeoutError:
|
||||
break
|
||||
except (
|
||||
httpx_ws.WebSocketDisconnect,
|
||||
httpx_ws.WebSocketNetworkError,
|
||||
httpx.ReadError,
|
||||
httpx.RemoteProtocolError,
|
||||
) as exc:
|
||||
await self._close_ws()
|
||||
if reconnect_attempts > 0 and not sent:
|
||||
reconnect_attempts -= 1
|
||||
continue
|
||||
_emit_error(
|
||||
ExecutionError(
|
||||
name="Disconnected",
|
||||
value=f"kernel WebSocket closed: {exc}",
|
||||
)
|
||||
)
|
||||
execution.timed_out = True
|
||||
break
|
||||
|
||||
if not saw_idle and execution.error is None:
|
||||
execution.timed_out = True
|
||||
_emit_error(
|
||||
ExecutionError(
|
||||
name="Timeout",
|
||||
value=f"run_code exceeded {timeout}s",
|
||||
)
|
||||
)
|
||||
|
||||
return execution
|
||||
|
||||
async def __aexit__(self, *args) -> None:
|
||||
await self.close()
|
||||
await super().__aexit__(*args)
|
||||
358
src/wrenn/code_runner/capsule.py
Normal file
358
src/wrenn/code_runner/capsule.py
Normal file
@ -0,0 +1,358 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import httpx_ws
|
||||
|
||||
from wrenn.capsule import Capsule as BaseCapsule
|
||||
from wrenn.capsule import _build_http_proxy_url
|
||||
from wrenn.code_runner._protocol import (
|
||||
apply_kernel_message,
|
||||
build_execute_request,
|
||||
build_ws_url,
|
||||
pick_kernel_id,
|
||||
validate_language,
|
||||
)
|
||||
from wrenn.code_runner.models import (
|
||||
Execution,
|
||||
ExecutionError,
|
||||
Result,
|
||||
)
|
||||
|
||||
DEFAULT_TEMPLATE = "code-runner-beta"
|
||||
DEFAULT_KERNEL = "wrenn"
|
||||
|
||||
|
||||
class Capsule(BaseCapsule):
|
||||
"""Code runner capsule with ``run_code`` support.
|
||||
|
||||
Uses ``code-runner-beta`` template and the ``wrenn`` Jupyter
|
||||
kernelspec by default::
|
||||
|
||||
from wrenn.code_runner import Capsule
|
||||
|
||||
capsule = Capsule()
|
||||
result = capsule.run_code("print('hello')")
|
||||
print(result.logs.stdout) # ["hello\\n"]
|
||||
"""
|
||||
|
||||
_kernel_id: str | None
|
||||
_kernel_name: str
|
||||
_proxy_client: httpx.Client | None
|
||||
_ws: httpx_ws.WebSocketSession | None
|
||||
_ws_cm: Any
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout: int | None = None,
|
||||
*,
|
||||
kernel: str | None = None,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
"""Create a code runner capsule.
|
||||
|
||||
Args:
|
||||
template (str | None): Template to boot from. Defaults to
|
||||
``"code-runner-beta"``.
|
||||
vcpus (int | None): Number of virtual CPUs.
|
||||
memory_mb (int | None): Memory in MiB.
|
||||
timeout (int | None): Inactivity TTL in seconds before auto-pause.
|
||||
kernel (str | None): Jupyter kernelspec name. Defaults to
|
||||
``"wrenn"``.
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
"""
|
||||
# Set attrs before super().__init__ so __del__ never sees a
|
||||
# half-constructed instance if creation fails.
|
||||
self._kernel_id = None
|
||||
self._kernel_name = kernel or DEFAULT_KERNEL
|
||||
self._proxy_client = None
|
||||
self._ws = None
|
||||
self._ws_cm = None
|
||||
super().__init__(
|
||||
template=template or DEFAULT_TEMPLATE,
|
||||
vcpus=vcpus,
|
||||
memory_mb=memory_mb,
|
||||
timeout=timeout,
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def _close_ws(self) -> None:
|
||||
cm = getattr(self, "_ws_cm", None)
|
||||
if cm is not None:
|
||||
try:
|
||||
cm.__exit__(None, None, None)
|
||||
except Exception:
|
||||
pass
|
||||
self._ws = None
|
||||
self._ws_cm = None
|
||||
|
||||
def _get_ws(self, kernel_id: str) -> httpx_ws.WebSocketSession:
|
||||
if self._ws is not None:
|
||||
return self._ws
|
||||
ws_url = build_ws_url(
|
||||
self._client._base_url,
|
||||
self._id,
|
||||
kernel_id,
|
||||
self._client._proxy_domain,
|
||||
)
|
||||
headers = {"X-API-Key": self._client._api_key}
|
||||
cm: Any = httpx_ws.connect_ws(ws_url, headers=headers)
|
||||
try:
|
||||
ws = cm.__enter__()
|
||||
except BaseException:
|
||||
try:
|
||||
cm.__exit__(None, None, None)
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
self._ws_cm = cm
|
||||
self._ws = ws
|
||||
return ws
|
||||
|
||||
def close(self) -> None:
|
||||
self._close_ws()
|
||||
proxy = getattr(self, "_proxy_client", None)
|
||||
if proxy is not None:
|
||||
try:
|
||||
proxy.close()
|
||||
except Exception:
|
||||
pass
|
||||
self._proxy_client = None
|
||||
|
||||
def __del__(self) -> None:
|
||||
try:
|
||||
self.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _instance_destroy(self, wait: bool = False) -> None:
|
||||
# Release WS threads + proxy client before destroying.
|
||||
# httpx_ws sync sessions spawn non-daemon threads; not joining
|
||||
# them keeps the interpreter alive after tests/scripts return.
|
||||
self.close()
|
||||
super()._instance_destroy(wait=wait)
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls,
|
||||
template: str | None = None,
|
||||
vcpus: int | None = None,
|
||||
memory_mb: int | None = None,
|
||||
timeout: int | None = None,
|
||||
*,
|
||||
kernel: str | None = None,
|
||||
wait: bool = False,
|
||||
api_key: str | None = None,
|
||||
base_url: str | None = None,
|
||||
) -> Capsule:
|
||||
"""Create a new code runner capsule.
|
||||
|
||||
Args:
|
||||
template (str | None): Template to boot from. Defaults to
|
||||
``"code-runner-beta"``.
|
||||
vcpus (int | None): Number of virtual CPUs.
|
||||
memory_mb (int | None): Memory in MiB.
|
||||
timeout (int | None): Inactivity TTL in seconds before auto-pause.
|
||||
kernel (str | None): Jupyter kernelspec name. Defaults to
|
||||
``"wrenn"``.
|
||||
wait (bool): Block until the capsule reaches ``running`` status.
|
||||
api_key (str | None): Wrenn API key. Falls back to
|
||||
``WRENN_API_KEY`` env var.
|
||||
base_url (str | None): API base URL override.
|
||||
|
||||
Returns:
|
||||
Capsule: A new code runner capsule instance.
|
||||
"""
|
||||
return cls(
|
||||
template=template or DEFAULT_TEMPLATE,
|
||||
vcpus=vcpus,
|
||||
memory_mb=memory_mb,
|
||||
timeout=timeout,
|
||||
kernel=kernel,
|
||||
wait=wait,
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
)
|
||||
|
||||
def _get_proxy_client(self) -> httpx.Client:
|
||||
if self._proxy_client is None:
|
||||
url = _build_http_proxy_url(
|
||||
self._client._base_url,
|
||||
self._id,
|
||||
8888,
|
||||
self._client._proxy_domain,
|
||||
)
|
||||
self._proxy_client = httpx.Client(
|
||||
base_url=url,
|
||||
headers={"X-API-Key": self._client._api_key},
|
||||
)
|
||||
return self._proxy_client
|
||||
|
||||
def _ensure_kernel(self, jupyter_timeout: float = 30) -> str:
|
||||
if self._kernel_id is not None:
|
||||
return self._kernel_id
|
||||
|
||||
client = self._get_proxy_client()
|
||||
deadline = time.monotonic() + jupyter_timeout
|
||||
last_exc: Exception | None = None
|
||||
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
# Try to reuse an existing kernel of the requested kernelspec.
|
||||
resp = client.get("/api/kernels")
|
||||
if resp.status_code < 500:
|
||||
resp.raise_for_status()
|
||||
matched = pick_kernel_id(resp.json(), self._kernel_name)
|
||||
if matched is not None:
|
||||
self._kernel_id = matched
|
||||
return matched
|
||||
# No matching kernel; create one with the requested spec.
|
||||
resp = client.post(
|
||||
"/api/kernels",
|
||||
json={"name": self._kernel_name},
|
||||
)
|
||||
if resp.status_code < 500:
|
||||
resp.raise_for_status()
|
||||
self._kernel_id = resp.json()["id"]
|
||||
return self._kernel_id
|
||||
last_exc = httpx.HTTPStatusError(
|
||||
f"Jupyter returned {resp.status_code}",
|
||||
request=resp.request,
|
||||
response=resp,
|
||||
)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
if exc.response.status_code < 500:
|
||||
raise
|
||||
last_exc = exc
|
||||
except Exception as exc:
|
||||
last_exc = exc
|
||||
time.sleep(0.5)
|
||||
|
||||
raise TimeoutError(
|
||||
f"Jupyter not available within {jupyter_timeout}s: {last_exc}"
|
||||
)
|
||||
|
||||
def run_code(
|
||||
self,
|
||||
code: str,
|
||||
language: str = "python",
|
||||
timeout: float = 30,
|
||||
jupyter_timeout: float = 30,
|
||||
on_result: Callable[[Result], Any] | None = None,
|
||||
on_stdout: Callable[[str], Any] | None = None,
|
||||
on_stderr: Callable[[str], Any] | None = None,
|
||||
on_error: Callable[[ExecutionError], Any] | None = None,
|
||||
) -> Execution:
|
||||
"""Execute code in a persistent Jupyter kernel.
|
||||
|
||||
Variables, imports, and function definitions survive across calls.
|
||||
|
||||
Args:
|
||||
code: Code string to execute.
|
||||
language: Execution backend language. Currently only ``"python"``
|
||||
is supported; passing anything else raises ``ValueError``.
|
||||
To target a non-Python kernel, set ``kernel=`` on the
|
||||
capsule constructor.
|
||||
timeout: Maximum seconds to wait for execution to complete.
|
||||
jupyter_timeout: Maximum seconds to wait for Jupyter to become
|
||||
available.
|
||||
on_result: Called for each rich output (charts, images, expression
|
||||
values).
|
||||
on_stdout: Called for each stdout chunk.
|
||||
on_stderr: Called for each stderr chunk.
|
||||
on_error: Called when the cell raises an exception.
|
||||
|
||||
Returns:
|
||||
An :class:`Execution` with ``.results``, ``.logs``, ``.error``,
|
||||
and a convenience ``.text`` property.
|
||||
"""
|
||||
validate_language(language)
|
||||
kernel_id = self._ensure_kernel(jupyter_timeout=jupyter_timeout)
|
||||
|
||||
msg = build_execute_request(code)
|
||||
msg_id = msg["header"]["msg_id"]
|
||||
|
||||
execution = Execution()
|
||||
deadline = time.monotonic() + timeout
|
||||
saw_idle = False
|
||||
|
||||
def _emit_error(err: ExecutionError) -> None:
|
||||
execution.error = err
|
||||
if on_error is not None:
|
||||
on_error(err)
|
||||
|
||||
reconnect_attempts = 1
|
||||
sent = False
|
||||
while True:
|
||||
try:
|
||||
ws = self._get_ws(kernel_id)
|
||||
if not sent:
|
||||
ws.send_text(json.dumps(msg))
|
||||
sent = True
|
||||
while True:
|
||||
time_left = deadline - time.monotonic()
|
||||
if time_left <= 0:
|
||||
break
|
||||
data = ws.receive_json(timeout=time_left)
|
||||
if not data:
|
||||
break
|
||||
if apply_kernel_message(
|
||||
data,
|
||||
msg_id,
|
||||
execution,
|
||||
_emit_error,
|
||||
on_result,
|
||||
on_stdout,
|
||||
on_stderr,
|
||||
):
|
||||
saw_idle = True
|
||||
break
|
||||
break
|
||||
except TimeoutError:
|
||||
break
|
||||
except (
|
||||
httpx_ws.WebSocketDisconnect,
|
||||
httpx_ws.WebSocketNetworkError,
|
||||
httpx.ReadError,
|
||||
httpx.RemoteProtocolError,
|
||||
) as exc:
|
||||
self._close_ws()
|
||||
if reconnect_attempts > 0 and not sent:
|
||||
reconnect_attempts -= 1
|
||||
continue
|
||||
_emit_error(
|
||||
ExecutionError(
|
||||
name="Disconnected",
|
||||
value=f"kernel WebSocket closed: {exc}",
|
||||
)
|
||||
)
|
||||
execution.timed_out = True
|
||||
break
|
||||
|
||||
if not saw_idle and execution.error is None:
|
||||
execution.timed_out = True
|
||||
_emit_error(
|
||||
ExecutionError(
|
||||
name="Timeout",
|
||||
value=f"run_code exceeded {timeout}s",
|
||||
)
|
||||
)
|
||||
|
||||
return execution
|
||||
|
||||
def __exit__(self, *args) -> None:
|
||||
self.close()
|
||||
super().__exit__(*args)
|
||||
149
src/wrenn/code_runner/models.py
Normal file
149
src/wrenn/code_runner/models.py
Normal file
@ -0,0 +1,149 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
_MIME_MAP: dict[str, str] = {
|
||||
"text/plain": "text",
|
||||
"text/html": "html",
|
||||
"text/markdown": "markdown",
|
||||
"image/svg+xml": "svg",
|
||||
"image/png": "png",
|
||||
"image/jpeg": "jpeg",
|
||||
"image/gif": "gif",
|
||||
"application/pdf": "pdf",
|
||||
"text/latex": "latex",
|
||||
"application/json": "json",
|
||||
"application/javascript": "javascript",
|
||||
"application/vnd.plotly.v1+json": "plotly",
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExecutionError:
|
||||
"""Error raised during code execution.
|
||||
|
||||
Attributes:
|
||||
name: Exception class name (e.g. ``"NameError"``).
|
||||
value: Exception message.
|
||||
traceback: Full traceback string.
|
||||
"""
|
||||
|
||||
name: str = ""
|
||||
value: str = ""
|
||||
traceback: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class Logs:
|
||||
"""Captured stdout/stderr streams.
|
||||
|
||||
Each element in the list is one chunk of text as it arrived from
|
||||
the kernel.
|
||||
"""
|
||||
|
||||
stdout: list[str] = field(default_factory=list)
|
||||
stderr: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Result:
|
||||
"""A single rich output from code execution.
|
||||
|
||||
Jupyter cells can produce multiple outputs — one ``execute_result``
|
||||
(the expression value) and zero or more ``display_data`` messages
|
||||
(from ``plt.show()``, ``display()``, etc.). Each becomes a
|
||||
``Result``.
|
||||
|
||||
Known MIME types are unpacked into named attributes; anything else
|
||||
lands in :pyattr:`extra`.
|
||||
"""
|
||||
|
||||
# --- MIME type fields ---
|
||||
text: str | None = None
|
||||
"""``text/plain`` representation."""
|
||||
html: str | None = None
|
||||
"""``text/html`` representation."""
|
||||
markdown: str | None = None
|
||||
"""``text/markdown`` representation."""
|
||||
svg: str | None = None
|
||||
"""``image/svg+xml`` representation."""
|
||||
png: str | None = None
|
||||
"""``image/png`` — base64-encoded."""
|
||||
jpeg: str | None = None
|
||||
"""``image/jpeg`` — base64-encoded."""
|
||||
gif: str | None = None
|
||||
"""``image/gif`` — base64-encoded."""
|
||||
pdf: str | None = None
|
||||
"""``application/pdf`` — base64-encoded."""
|
||||
latex: str | None = None
|
||||
"""``text/latex`` representation."""
|
||||
json: dict | None = None
|
||||
"""``application/json`` representation."""
|
||||
javascript: str | None = None
|
||||
"""``application/javascript`` representation."""
|
||||
plotly: dict | None = None
|
||||
"""``application/vnd.plotly.v1+json`` representation."""
|
||||
extra: dict[str, str] | None = None
|
||||
"""MIME types not covered by the named fields above."""
|
||||
|
||||
is_main_result: bool = False
|
||||
"""``True`` when this came from an ``execute_result`` message
|
||||
(i.e. the value of the last expression in the cell). ``False``
|
||||
for ``display_data`` outputs."""
|
||||
|
||||
@classmethod
|
||||
def from_bundle(
|
||||
cls, bundle: dict[str, str], *, is_main_result: bool = False
|
||||
) -> Result:
|
||||
"""Build a ``Result`` from a Jupyter MIME bundle dict."""
|
||||
kwargs: dict = {"is_main_result": is_main_result}
|
||||
extra: dict[str, str] = {}
|
||||
for mime, value in bundle.items():
|
||||
attr = _MIME_MAP.get(mime)
|
||||
if attr is not None:
|
||||
kwargs[attr] = value
|
||||
else:
|
||||
extra[mime] = value
|
||||
if extra:
|
||||
kwargs["extra"] = extra
|
||||
return cls(**kwargs)
|
||||
|
||||
def formats(self) -> list[str]:
|
||||
"""Return names of non-``None`` MIME-type fields."""
|
||||
out: list[str] = [
|
||||
attr for attr in _MIME_MAP.values() if getattr(self, attr) is not None
|
||||
]
|
||||
if self.extra:
|
||||
out.extend(self.extra)
|
||||
return out
|
||||
|
||||
|
||||
@dataclass
|
||||
class Execution:
|
||||
"""Complete result of a ``run_code`` call.
|
||||
|
||||
Attributes:
|
||||
results: All rich outputs produced by the cell — charts, tables,
|
||||
images, expression values, etc.
|
||||
logs: Captured stdout/stderr text.
|
||||
error: Populated when the cell raised an exception.
|
||||
execution_count: Jupyter execution counter (the ``[N]`` number).
|
||||
"""
|
||||
|
||||
results: list[Result] = field(default_factory=list)
|
||||
logs: Logs = field(default_factory=Logs)
|
||||
error: ExecutionError | None = None
|
||||
execution_count: int | None = None
|
||||
timed_out: bool = False
|
||||
"""``True`` when execution was cut short by the ``timeout`` parameter
|
||||
(or by the kernel WebSocket dropping). Pairs with ``error`` of name
|
||||
``"Timeout"`` or ``"Disconnected"``."""
|
||||
|
||||
@property
|
||||
def text(self) -> str | None:
|
||||
"""Convenience — ``text/plain`` of the main ``execute_result``,
|
||||
or ``None`` if the cell had no expression value."""
|
||||
for r in self.results:
|
||||
if r.is_main_result:
|
||||
return r.text
|
||||
return None
|
||||
496
src/wrenn/commands.py
Normal file
496
src/wrenn/commands.py
Normal file
@ -0,0 +1,496 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import builtins
|
||||
import json
|
||||
from collections.abc import AsyncIterator, Iterator
|
||||
from dataclasses import dataclass
|
||||
from typing import Literal, overload
|
||||
|
||||
import httpx
|
||||
import httpx_ws
|
||||
|
||||
from wrenn.exceptions import handle_response
|
||||
|
||||
# Both signal a terminated WebSocket: ``WebSocketDisconnect`` is a clean close,
|
||||
# ``WebSocketNetworkError`` an abrupt one. The Wrenn server closes exec/process
|
||||
# streams abruptly, so iterators must treat either as end-of-stream.
|
||||
_WS_CLOSED = (httpx_ws.WebSocketDisconnect, httpx_ws.WebSocketNetworkError)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CommandResult:
|
||||
"""Result from a foreground command execution."""
|
||||
|
||||
stdout: str
|
||||
stderr: str
|
||||
exit_code: int
|
||||
duration_ms: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class CommandHandle:
|
||||
"""Handle for a background process."""
|
||||
|
||||
pid: int
|
||||
tag: str
|
||||
capsule_id: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProcessInfo:
|
||||
"""Information about a running process."""
|
||||
|
||||
pid: int
|
||||
tag: str | None = None
|
||||
cmd: str | None = None
|
||||
args: list[str] | None = None
|
||||
|
||||
|
||||
class StreamEvent:
|
||||
"""Base class for streaming exec events."""
|
||||
|
||||
__slots__ = ("type",)
|
||||
|
||||
def __init__(self, type: str) -> None:
|
||||
self.type = type
|
||||
|
||||
|
||||
class StreamStartEvent(StreamEvent):
|
||||
__slots__ = ("pid",)
|
||||
|
||||
def __init__(self, pid: int) -> None:
|
||||
super().__init__("start")
|
||||
self.pid = pid
|
||||
|
||||
|
||||
class StreamStdoutEvent(StreamEvent):
|
||||
__slots__ = ("data",)
|
||||
|
||||
def __init__(self, data: str) -> None:
|
||||
super().__init__("stdout")
|
||||
self.data = data
|
||||
|
||||
|
||||
class StreamStderrEvent(StreamEvent):
|
||||
__slots__ = ("data",)
|
||||
|
||||
def __init__(self, data: str) -> None:
|
||||
super().__init__("stderr")
|
||||
self.data = data
|
||||
|
||||
|
||||
class StreamExitEvent(StreamEvent):
|
||||
__slots__ = ("exit_code",)
|
||||
|
||||
def __init__(self, exit_code: int) -> None:
|
||||
super().__init__("exit")
|
||||
self.exit_code = exit_code
|
||||
|
||||
|
||||
class StreamErrorEvent(StreamEvent):
|
||||
__slots__ = ("data",)
|
||||
|
||||
def __init__(self, data: str) -> None:
|
||||
super().__init__("error")
|
||||
self.data = data
|
||||
|
||||
|
||||
def _parse_stream_event(raw: dict) -> StreamEvent:
|
||||
t = raw.get("type")
|
||||
if t == "start":
|
||||
return StreamStartEvent(pid=raw.get("pid", 0))
|
||||
if t == "stdout":
|
||||
return StreamStdoutEvent(data=raw.get("data", ""))
|
||||
if t == "stderr":
|
||||
return StreamStderrEvent(data=raw.get("data", ""))
|
||||
if t == "exit":
|
||||
return StreamExitEvent(exit_code=raw.get("exit_code", -1))
|
||||
if t == "error":
|
||||
return StreamErrorEvent(data=raw.get("data", ""))
|
||||
return StreamEvent(type=t or "unknown")
|
||||
|
||||
|
||||
def _build_exec_payload(
|
||||
cmd: str,
|
||||
background: bool,
|
||||
timeout: int | None,
|
||||
envs: dict[str, str] | None,
|
||||
cwd: str | None,
|
||||
tag: str | None,
|
||||
) -> dict:
|
||||
payload: dict = {
|
||||
"cmd": "/bin/sh",
|
||||
"args": ["-c", cmd],
|
||||
"background": background,
|
||||
}
|
||||
if timeout is not None and not background:
|
||||
payload["timeout_sec"] = timeout
|
||||
if envs is not None:
|
||||
payload["envs"] = envs
|
||||
if cwd is not None:
|
||||
payload["cwd"] = cwd
|
||||
if tag is not None:
|
||||
payload["tag"] = tag
|
||||
return payload
|
||||
|
||||
|
||||
def _exec_http_timeout(background: bool, timeout: int | None) -> httpx.Timeout | None:
|
||||
if not background and timeout is not None:
|
||||
return httpx.Timeout(timeout + 10, connect=5.0)
|
||||
return None
|
||||
|
||||
|
||||
def _decode_exec_run(
|
||||
data: dict, capsule_id: str, background: bool
|
||||
) -> CommandResult | CommandHandle:
|
||||
if background:
|
||||
return CommandHandle(
|
||||
pid=data.get("pid", 0),
|
||||
tag=data.get("tag", ""),
|
||||
capsule_id=capsule_id,
|
||||
)
|
||||
return _decode_exec_response(data)
|
||||
|
||||
|
||||
def _build_stream_start(cmd: str, args: builtins.list[str] | None) -> dict:
|
||||
if args:
|
||||
return {"type": "start", "cmd": cmd, "args": args}
|
||||
return {"type": "start", "cmd": "/bin/sh", "args": ["-c", cmd]}
|
||||
|
||||
|
||||
def _decode_exec_response(data: dict) -> CommandResult:
|
||||
stdout = data.get("stdout") or ""
|
||||
stderr = data.get("stderr") or ""
|
||||
if data.get("encoding") == "base64":
|
||||
stdout = base64.b64decode(stdout).decode("utf-8", errors="replace")
|
||||
if stderr:
|
||||
stderr = base64.b64decode(stderr).decode("utf-8", errors="replace")
|
||||
return CommandResult(
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
exit_code=data.get("exit_code", -1),
|
||||
duration_ms=data.get("duration_ms"),
|
||||
)
|
||||
|
||||
|
||||
class Commands:
|
||||
"""Sync command execution interface. Accessed via ``capsule.commands``."""
|
||||
|
||||
def __init__(self, capsule_id: str, http: httpx.Client) -> None:
|
||||
self._capsule_id = capsule_id
|
||||
self._http = http
|
||||
|
||||
@overload
|
||||
def run(
|
||||
self,
|
||||
cmd: str,
|
||||
*,
|
||||
background: Literal[False] = ...,
|
||||
timeout: int | None = 30,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
tag: str | None = None,
|
||||
) -> CommandResult: ...
|
||||
|
||||
@overload
|
||||
def run(
|
||||
self,
|
||||
cmd: str,
|
||||
*,
|
||||
background: Literal[True],
|
||||
timeout: int | None = 30,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
tag: str | None = None,
|
||||
) -> CommandHandle: ...
|
||||
|
||||
def run(
|
||||
self,
|
||||
cmd: str,
|
||||
*,
|
||||
background: bool = False,
|
||||
timeout: int | None = 30,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
tag: str | None = None,
|
||||
) -> CommandResult | CommandHandle:
|
||||
"""Execute a shell command inside the capsule.
|
||||
|
||||
Args:
|
||||
cmd (str): Shell command string to execute.
|
||||
background (bool): If ``True``, launch the process in the
|
||||
background and return a :class:`CommandHandle` immediately.
|
||||
Defaults to ``False``.
|
||||
timeout (int | None): Seconds before the foreground command times
|
||||
out. Ignored for background commands. Defaults to ``30``.
|
||||
envs (dict[str, str] | None): Additional environment variables
|
||||
to set for the process.
|
||||
cwd (str | None): Working directory for the process.
|
||||
tag (str | None): Optional label attached to background processes
|
||||
for later retrieval via :meth:`connect`.
|
||||
|
||||
Returns:
|
||||
CommandResult: stdout, stderr, exit code, and duration for
|
||||
foreground commands (``background=False``).
|
||||
|
||||
CommandHandle: PID and tag for background commands
|
||||
(``background=True``).
|
||||
"""
|
||||
resp = self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/exec",
|
||||
json=_build_exec_payload(cmd, background, timeout, envs, cwd, tag),
|
||||
timeout=_exec_http_timeout(background, timeout),
|
||||
)
|
||||
data = handle_response(resp)
|
||||
assert isinstance(data, dict)
|
||||
return _decode_exec_run(data, self._capsule_id, background)
|
||||
|
||||
def list(self) -> list[ProcessInfo]:
|
||||
"""List all running background processes in the capsule.
|
||||
|
||||
Returns:
|
||||
list[ProcessInfo]: Running processes with their PID, tag, and
|
||||
command information.
|
||||
"""
|
||||
resp = self._http.get(f"/v1/capsules/{self._capsule_id}/processes")
|
||||
data = handle_response(resp)
|
||||
assert isinstance(data, dict)
|
||||
return [
|
||||
ProcessInfo(
|
||||
pid=p.get("pid", 0),
|
||||
tag=p.get("tag"),
|
||||
cmd=p.get("cmd"),
|
||||
args=p.get("args"),
|
||||
)
|
||||
for p in data.get("processes", [])
|
||||
]
|
||||
|
||||
def kill(self, pid: int) -> None:
|
||||
"""Send SIGKILL to a background process.
|
||||
|
||||
Args:
|
||||
pid (int): PID of the process to kill.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no process with the given PID exists.
|
||||
"""
|
||||
resp = self._http.delete(f"/v1/capsules/{self._capsule_id}/processes/{pid}")
|
||||
handle_response(resp)
|
||||
|
||||
def connect(self, pid: int) -> Iterator[StreamEvent]:
|
||||
"""Connect to a running background process and stream its output.
|
||||
|
||||
Args:
|
||||
pid (int): PID of the background process to attach to.
|
||||
|
||||
Yields:
|
||||
StreamEvent: Successive output events. Stops on
|
||||
:class:`StreamExitEvent` or :class:`StreamErrorEvent`.
|
||||
"""
|
||||
with httpx_ws.connect_ws(
|
||||
f"/v1/capsules/{self._capsule_id}/processes/{pid}/stream",
|
||||
self._http,
|
||||
) as ws: # type: httpx_ws.WebSocketSession
|
||||
while True:
|
||||
try:
|
||||
raw = ws.receive_json()
|
||||
event = _parse_stream_event(raw)
|
||||
yield event
|
||||
if event.type in ("exit", "error"):
|
||||
break
|
||||
except _WS_CLOSED:
|
||||
break
|
||||
|
||||
def stream(
|
||||
self, cmd: str, args: builtins.list[str] | None = None
|
||||
) -> Iterator[StreamEvent]:
|
||||
"""Execute a command via WebSocket, streaming output as events.
|
||||
|
||||
Args:
|
||||
cmd (str): Command to execute.
|
||||
args (list[str] | None): Additional arguments for the command.
|
||||
When omitted, *cmd* is interpreted as a shell command
|
||||
string and executed via ``/bin/sh -c``.
|
||||
|
||||
Yields:
|
||||
StreamEvent: Successive events including :class:`StreamStartEvent`,
|
||||
:class:`StreamStdoutEvent`, :class:`StreamStderrEvent`,
|
||||
:class:`StreamExitEvent`, and :class:`StreamErrorEvent`.
|
||||
"""
|
||||
with httpx_ws.connect_ws(
|
||||
f"/v1/capsules/{self._capsule_id}/exec/stream",
|
||||
self._http,
|
||||
) as ws: # type: httpx_ws.WebSocketSession
|
||||
ws.send_text(json.dumps(_build_stream_start(cmd, args)))
|
||||
while True:
|
||||
try:
|
||||
raw = ws.receive_json()
|
||||
event = _parse_stream_event(raw)
|
||||
yield event
|
||||
if event.type in ("exit", "error"):
|
||||
break
|
||||
except _WS_CLOSED:
|
||||
break
|
||||
|
||||
|
||||
class AsyncCommands:
|
||||
"""Async command execution interface. Accessed via ``capsule.commands``."""
|
||||
|
||||
def __init__(self, capsule_id: str, http: httpx.AsyncClient) -> None:
|
||||
self._capsule_id = capsule_id
|
||||
self._http = http
|
||||
|
||||
@overload
|
||||
async def run(
|
||||
self,
|
||||
cmd: str,
|
||||
*,
|
||||
background: Literal[False] = ...,
|
||||
timeout: int | None = 30,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
tag: str | None = None,
|
||||
) -> CommandResult: ...
|
||||
|
||||
@overload
|
||||
async def run(
|
||||
self,
|
||||
cmd: str,
|
||||
*,
|
||||
background: Literal[True],
|
||||
timeout: int | None = 30,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
tag: str | None = None,
|
||||
) -> CommandHandle: ...
|
||||
|
||||
async def run(
|
||||
self,
|
||||
cmd: str,
|
||||
*,
|
||||
background: bool = False,
|
||||
timeout: int | None = 30,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
tag: str | None = None,
|
||||
) -> CommandResult | CommandHandle:
|
||||
"""Execute a shell command inside the capsule.
|
||||
|
||||
Args:
|
||||
cmd (str): Shell command string to execute.
|
||||
background (bool): If ``True``, launch the process in the
|
||||
background and return a :class:`CommandHandle` immediately.
|
||||
Defaults to ``False``.
|
||||
timeout (int | None): Seconds before the foreground command times
|
||||
out. Ignored for background commands. Defaults to ``30``.
|
||||
envs (dict[str, str] | None): Additional environment variables
|
||||
to set for the process.
|
||||
cwd (str | None): Working directory for the process.
|
||||
tag (str | None): Optional label attached to background processes
|
||||
for later retrieval via :meth:`connect`.
|
||||
|
||||
Returns:
|
||||
CommandResult: stdout, stderr, exit code, and duration for
|
||||
foreground commands (``background=False``).
|
||||
|
||||
CommandHandle: PID and tag for background commands
|
||||
(``background=True``).
|
||||
"""
|
||||
resp = await self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/exec",
|
||||
json=_build_exec_payload(cmd, background, timeout, envs, cwd, tag),
|
||||
timeout=_exec_http_timeout(background, timeout),
|
||||
)
|
||||
data = handle_response(resp)
|
||||
assert isinstance(data, dict)
|
||||
return _decode_exec_run(data, self._capsule_id, background)
|
||||
|
||||
async def list(self) -> list[ProcessInfo]:
|
||||
"""List all running background processes in the capsule.
|
||||
|
||||
Returns:
|
||||
list[ProcessInfo]: Running processes with their PID, tag, and
|
||||
command information.
|
||||
"""
|
||||
resp = await self._http.get(f"/v1/capsules/{self._capsule_id}/processes")
|
||||
data = handle_response(resp)
|
||||
assert isinstance(data, dict)
|
||||
return [
|
||||
ProcessInfo(
|
||||
pid=p.get("pid", 0),
|
||||
tag=p.get("tag"),
|
||||
cmd=p.get("cmd"),
|
||||
args=p.get("args"),
|
||||
)
|
||||
for p in data.get("processes", [])
|
||||
]
|
||||
|
||||
async def kill(self, pid: int) -> None:
|
||||
"""Send SIGKILL to a background process.
|
||||
|
||||
Args:
|
||||
pid (int): PID of the process to kill.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If no process with the given PID exists.
|
||||
"""
|
||||
resp = await self._http.delete(
|
||||
f"/v1/capsules/{self._capsule_id}/processes/{pid}"
|
||||
)
|
||||
handle_response(resp)
|
||||
|
||||
async def connect(self, pid: int) -> AsyncIterator[StreamEvent]:
|
||||
"""Connect to a running background process and stream its output.
|
||||
|
||||
Args:
|
||||
pid (int): PID of the background process to attach to.
|
||||
|
||||
Yields:
|
||||
StreamEvent: Successive output events. Stops on
|
||||
:class:`StreamExitEvent` or :class:`StreamErrorEvent`.
|
||||
"""
|
||||
async with httpx_ws.aconnect_ws(
|
||||
f"/v1/capsules/{self._capsule_id}/processes/{pid}/stream",
|
||||
self._http,
|
||||
) as ws: # type: httpx_ws.AsyncWebSocketSession
|
||||
try:
|
||||
while True:
|
||||
raw = await ws.receive_json()
|
||||
event = _parse_stream_event(raw)
|
||||
yield event
|
||||
if event.type in ("exit", "error"):
|
||||
break
|
||||
except _WS_CLOSED:
|
||||
pass
|
||||
|
||||
async def stream(
|
||||
self, cmd: str, args: builtins.list[str] | None = None
|
||||
) -> AsyncIterator[StreamEvent]:
|
||||
"""Execute a command via WebSocket, streaming output as events.
|
||||
|
||||
Args:
|
||||
cmd (str): Command to execute.
|
||||
args (list[str] | None): Additional arguments for the command.
|
||||
When omitted, *cmd* is interpreted as a shell command
|
||||
string and executed via ``/bin/sh -c``.
|
||||
|
||||
Yields:
|
||||
StreamEvent: Successive events including :class:`StreamStartEvent`,
|
||||
:class:`StreamStdoutEvent`, :class:`StreamStderrEvent`,
|
||||
:class:`StreamExitEvent`, and :class:`StreamErrorEvent`.
|
||||
"""
|
||||
async with httpx_ws.aconnect_ws(
|
||||
f"/v1/capsules/{self._capsule_id}/exec/stream",
|
||||
self._http,
|
||||
) as ws: # type: httpx_ws.AsyncWebSocketSession
|
||||
await ws.send_text(json.dumps(_build_stream_start(cmd, args)))
|
||||
try:
|
||||
while True:
|
||||
raw = await ws.receive_json()
|
||||
event = _parse_stream_event(raw)
|
||||
yield event
|
||||
if event.type in ("exit", "error"):
|
||||
break
|
||||
except _WS_CLOSED:
|
||||
pass
|
||||
180
src/wrenn/exceptions.py
Normal file
180
src/wrenn/exceptions.py
Normal file
@ -0,0 +1,180 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import warnings
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
class WrennError(Exception):
|
||||
"""Base exception for all Wrenn SDK errors.
|
||||
|
||||
All SDK exceptions inherit from this class, so you can catch
|
||||
``WrennError`` to handle any API error generically.
|
||||
|
||||
Attributes:
|
||||
code (str): Machine-readable error code from the API
|
||||
(e.g. ``"not_found"``).
|
||||
message (str): Human-readable error description.
|
||||
status_code (int): HTTP status code of the response.
|
||||
"""
|
||||
|
||||
def __init__(self, code: str, message: str, status_code: int) -> None:
|
||||
"""Initialize a WrennError.
|
||||
|
||||
Args:
|
||||
code (str): Machine-readable error code.
|
||||
message (str): Human-readable error description.
|
||||
status_code (int): HTTP status code of the response.
|
||||
"""
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
class WrennValidationError(WrennError):
|
||||
"""400 — Invalid request parameters."""
|
||||
|
||||
|
||||
class WrennAuthenticationError(WrennError):
|
||||
"""401 — Invalid or missing authentication."""
|
||||
|
||||
|
||||
class WrennForbiddenError(WrennError):
|
||||
"""403 — Authenticated but not authorized."""
|
||||
|
||||
|
||||
class WrennNotFoundError(WrennError):
|
||||
"""404 — Resource not found."""
|
||||
|
||||
|
||||
class WrennConflictError(WrennError):
|
||||
"""409 — State conflict (e.g. invalid_state)."""
|
||||
|
||||
|
||||
class WrennHostHasCapsulesError(WrennConflictError):
|
||||
"""409 — Host still has running capsules.
|
||||
|
||||
Attributes:
|
||||
capsule_ids (list[str]): IDs of the capsules still running on the host.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, code: str, message: str, status_code: int, capsule_ids: list[str]
|
||||
) -> None:
|
||||
"""Initialize a WrennHostHasCapsulesError.
|
||||
|
||||
Args:
|
||||
code (str): Machine-readable error code.
|
||||
message (str): Human-readable error description.
|
||||
status_code (int): HTTP status code of the response.
|
||||
capsule_ids (list[str]): IDs of capsules still on the host.
|
||||
"""
|
||||
self.capsule_ids = capsule_ids
|
||||
super().__init__(code, message, status_code)
|
||||
|
||||
@property
|
||||
def sandbox_ids(self) -> list[str]:
|
||||
warnings.warn(
|
||||
"'sandbox_ids' is deprecated, use 'capsule_ids' instead",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return self.capsule_ids
|
||||
|
||||
|
||||
class WrennHostUnavailableError(WrennError):
|
||||
"""503 — No suitable host available."""
|
||||
|
||||
|
||||
class WrennAgentError(WrennError):
|
||||
"""502 — Host agent returned an error."""
|
||||
|
||||
|
||||
class WrennInternalError(WrennError):
|
||||
"""500 — Unexpected server error."""
|
||||
|
||||
|
||||
_ERROR_MAP: dict[str, type[WrennError]] = {
|
||||
"invalid_request": WrennValidationError,
|
||||
"unauthorized": WrennAuthenticationError,
|
||||
"forbidden": WrennForbiddenError,
|
||||
"not_found": WrennNotFoundError,
|
||||
"invalid_state": WrennConflictError,
|
||||
"conflict": WrennConflictError,
|
||||
"host_has_sandboxes": WrennHostHasCapsulesError,
|
||||
"host_has_capsules": WrennHostHasCapsulesError,
|
||||
"host_unavailable": WrennHostUnavailableError,
|
||||
"agent_error": WrennAgentError,
|
||||
"internal_error": WrennInternalError,
|
||||
}
|
||||
|
||||
|
||||
def _raise_for_status(resp: httpx.Response) -> None:
|
||||
if resp.status_code < 400:
|
||||
return
|
||||
|
||||
try:
|
||||
body = resp.json()
|
||||
except Exception:
|
||||
raise WrennInternalError(
|
||||
code="internal_error",
|
||||
message=resp.text or f"HTTP {resp.status_code}",
|
||||
status_code=resp.status_code,
|
||||
)
|
||||
|
||||
err = body.get("error", {})
|
||||
code = err.get("code", "internal_error")
|
||||
message = err.get("message", resp.text)
|
||||
|
||||
exc_cls = _ERROR_MAP.get(code, WrennError)
|
||||
|
||||
if exc_cls is WrennHostHasCapsulesError:
|
||||
raise WrennHostHasCapsulesError(
|
||||
code=code,
|
||||
message=message,
|
||||
status_code=resp.status_code,
|
||||
capsule_ids=body.get("capsule_ids") or body.get("sandbox_ids", []),
|
||||
)
|
||||
|
||||
raise exc_cls(
|
||||
code=code,
|
||||
message=message,
|
||||
status_code=resp.status_code,
|
||||
)
|
||||
|
||||
|
||||
def handle_response(resp: httpx.Response) -> dict | list:
|
||||
_raise_for_status(resp)
|
||||
|
||||
if resp.status_code == 204:
|
||||
return {}
|
||||
|
||||
if not resp.content:
|
||||
return {}
|
||||
|
||||
return resp.json()
|
||||
|
||||
|
||||
def __getattr__(name: str) -> type:
|
||||
if name == "WrennHostHasSandboxesError":
|
||||
warnings.warn(
|
||||
"'WrennHostHasSandboxesError' is deprecated, use 'WrennHostHasCapsulesError' instead",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return WrennHostHasCapsulesError
|
||||
if name in ("GitError", "GitCommandError", "GitAuthError"):
|
||||
from wrenn._git.exceptions import (
|
||||
GitAuthError as _GitAuthError,
|
||||
GitCommandError as _GitCommandError,
|
||||
GitError as _GitError,
|
||||
)
|
||||
|
||||
_m: dict[str, type] = {
|
||||
"GitError": _GitError,
|
||||
"GitCommandError": _GitCommandError,
|
||||
"GitAuthError": _GitAuthError,
|
||||
}
|
||||
return _m[name]
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
448
src/wrenn/files.py
Normal file
448
src/wrenn/files.py
Normal file
@ -0,0 +1,448 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from collections.abc import AsyncIterator, Iterator
|
||||
|
||||
import httpx
|
||||
|
||||
from wrenn.exceptions import WrennNotFoundError, _raise_for_status, handle_response
|
||||
from wrenn.models import FileEntry, ListDirResponse, MakeDirResponse
|
||||
|
||||
|
||||
def _is_already_exists(resp: httpx.Response) -> bool:
|
||||
"""Detect server's already-exists reply across status codes / code strings.
|
||||
|
||||
Server may return 409 with code "conflict"/"already_exists" or wrap
|
||||
"already_exists" inside an "internal" 500 message.
|
||||
"""
|
||||
if resp.status_code < 400:
|
||||
return False
|
||||
try:
|
||||
body = resp.json()
|
||||
except Exception:
|
||||
return False
|
||||
err = body.get("error", {}) if isinstance(body, dict) else {}
|
||||
code = err.get("code", "")
|
||||
msg = err.get("message", "") or ""
|
||||
return code in {"conflict", "already_exists"} or "already_exists" in msg
|
||||
|
||||
|
||||
def _find_entry(list_fn, path: str) -> FileEntry | None:
|
||||
parent = os.path.dirname(path)
|
||||
name = os.path.basename(path)
|
||||
try:
|
||||
for entry in list_fn(parent, depth=1):
|
||||
if entry.name == name:
|
||||
return entry
|
||||
except WrennNotFoundError:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
async def _async_find_entry(list_fn, path: str) -> FileEntry | None:
|
||||
parent = os.path.dirname(path)
|
||||
name = os.path.basename(path)
|
||||
try:
|
||||
for entry in await list_fn(parent, depth=1):
|
||||
if entry.name == name:
|
||||
return entry
|
||||
except WrennNotFoundError:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
_MULTIPART_FILE_HEADER = (
|
||||
b'Content-Disposition: form-data; name="file"; filename="upload.bin"\r\n'
|
||||
b"Content-Type: application/octet-stream\r\n\r\n"
|
||||
)
|
||||
|
||||
|
||||
def _multipart_frame(path: str, boundary: bytes) -> tuple[bytes, bytes]:
|
||||
"""Return (preamble, trailer) bytes wrapping the file body chunks."""
|
||||
preamble = (
|
||||
b"--" + boundary + b"\r\n"
|
||||
b'Content-Disposition: form-data; name="path"\r\n\r\n'
|
||||
+ path.encode("utf-8")
|
||||
+ b"\r\n--"
|
||||
+ boundary
|
||||
+ b"\r\n"
|
||||
+ _MULTIPART_FILE_HEADER
|
||||
)
|
||||
trailer = b"\r\n--" + boundary + b"--\r\n"
|
||||
return preamble, trailer
|
||||
|
||||
|
||||
def _multipart_headers(boundary: bytes) -> dict[str, str]:
|
||||
return {
|
||||
"Content-Type": f"multipart/form-data; boundary={boundary.decode('utf-8')}",
|
||||
"Transfer-Encoding": "chunked",
|
||||
}
|
||||
|
||||
|
||||
class Files:
|
||||
"""Sync filesystem interface. Accessed via ``capsule.files``."""
|
||||
|
||||
def __init__(self, capsule_id: str, http: httpx.Client) -> None:
|
||||
self._capsule_id = capsule_id
|
||||
self._http = http
|
||||
|
||||
def read(self, path: str) -> str:
|
||||
"""Read a file as a UTF-8 string.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the file inside the capsule.
|
||||
|
||||
Returns:
|
||||
str: File contents decoded as UTF-8.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
return self.read_bytes(path).decode("utf-8", errors="replace")
|
||||
|
||||
def read_bytes(self, path: str) -> bytes:
|
||||
"""Read a file as raw bytes.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the file inside the capsule.
|
||||
|
||||
Returns:
|
||||
bytes: Raw file contents.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
resp = self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/read",
|
||||
json={"path": path},
|
||||
)
|
||||
_raise_for_status(resp)
|
||||
return resp.content
|
||||
|
||||
def write(self, path: str, data: str | bytes) -> None:
|
||||
"""Write data to a file inside the capsule.
|
||||
|
||||
Creates parent directories if they do not exist.
|
||||
|
||||
Args:
|
||||
path (str): Absolute destination path inside the capsule.
|
||||
data (str | bytes): Content to write. Strings are UTF-8 encoded.
|
||||
"""
|
||||
if isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
resp = self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/write",
|
||||
files={"file": ("upload", data)},
|
||||
data={"path": path},
|
||||
)
|
||||
_raise_for_status(resp)
|
||||
|
||||
def list(self, path: str, depth: int = 1) -> list[FileEntry]:
|
||||
"""List directory contents.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the directory inside the capsule.
|
||||
depth (int): Recursion depth. ``1`` lists only immediate children.
|
||||
Defaults to ``1``.
|
||||
|
||||
Returns:
|
||||
list[FileEntry]: Entries in the directory.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
resp = self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/list",
|
||||
json={"path": path, "depth": depth},
|
||||
)
|
||||
parsed = ListDirResponse.model_validate(handle_response(resp))
|
||||
return parsed.entries or []
|
||||
|
||||
def exists(self, path: str) -> bool:
|
||||
"""Check whether a path exists inside the capsule.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to check.
|
||||
|
||||
Returns:
|
||||
bool: ``True`` if the path exists.
|
||||
"""
|
||||
parent = os.path.dirname(path)
|
||||
name = os.path.basename(path)
|
||||
try:
|
||||
entries = self.list(parent, depth=1)
|
||||
except WrennNotFoundError:
|
||||
return False
|
||||
return any(e.name == name for e in entries)
|
||||
|
||||
def make_dir(self, path: str) -> FileEntry:
|
||||
"""Create a directory (with parents). Idempotent.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path of the directory to create.
|
||||
|
||||
Returns:
|
||||
FileEntry: The created (or already-existing) directory entry.
|
||||
"""
|
||||
resp = self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/mkdir",
|
||||
json={"path": path},
|
||||
)
|
||||
if _is_already_exists(resp):
|
||||
existing = _find_entry(self.list, path)
|
||||
if existing is not None:
|
||||
return existing
|
||||
parsed = MakeDirResponse.model_validate(handle_response(resp))
|
||||
if parsed.entry is None:
|
||||
raise RuntimeError("mkdir response missing entry")
|
||||
return parsed.entry
|
||||
|
||||
def remove(self, path: str) -> None:
|
||||
"""Remove a file or directory recursively.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to remove.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
resp = self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/remove",
|
||||
json={"path": path},
|
||||
)
|
||||
handle_response(resp)
|
||||
|
||||
def upload_stream(self, path: str, stream: Iterator[bytes]) -> None:
|
||||
"""Stream a large file into the capsule.
|
||||
|
||||
Prefer this over :meth:`write` when the file is too large to hold in
|
||||
memory.
|
||||
|
||||
Args:
|
||||
path (str): Absolute destination path inside the capsule.
|
||||
stream (Iterator[bytes]): Iterable of byte chunks to upload.
|
||||
"""
|
||||
boundary = os.urandom(16).hex().encode("utf-8")
|
||||
preamble, trailer = _multipart_frame(path, boundary)
|
||||
|
||||
def _multipart() -> Iterator[bytes]:
|
||||
yield preamble
|
||||
for chunk in stream:
|
||||
yield chunk if isinstance(chunk, bytes) else chunk.encode("utf-8")
|
||||
yield trailer
|
||||
|
||||
resp = self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/stream/write",
|
||||
content=_multipart(),
|
||||
headers=_multipart_headers(boundary),
|
||||
)
|
||||
_raise_for_status(resp)
|
||||
|
||||
def download_stream(self, path: str) -> Iterator[bytes]:
|
||||
"""Stream a large file out of the capsule.
|
||||
|
||||
Prefer this over :meth:`read_bytes` when the file is too large to hold
|
||||
in memory.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the file inside the capsule.
|
||||
|
||||
Yields:
|
||||
bytes: Successive byte chunks of the file.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
with self._http.stream(
|
||||
"POST",
|
||||
f"/v1/capsules/{self._capsule_id}/files/stream/read",
|
||||
json={"path": path},
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
yield from resp.iter_bytes()
|
||||
|
||||
|
||||
class AsyncFiles:
|
||||
"""Async filesystem interface. Accessed via ``capsule.files``."""
|
||||
|
||||
def __init__(self, capsule_id: str, http: httpx.AsyncClient) -> None:
|
||||
self._capsule_id = capsule_id
|
||||
self._http = http
|
||||
|
||||
async def read(self, path: str) -> str:
|
||||
"""Read a file as a UTF-8 string.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the file inside the capsule.
|
||||
|
||||
Returns:
|
||||
str: File contents decoded as UTF-8.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
data = await self.read_bytes(path)
|
||||
return data.decode("utf-8", errors="replace")
|
||||
|
||||
async def read_bytes(self, path: str) -> bytes:
|
||||
"""Read a file as raw bytes.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the file inside the capsule.
|
||||
|
||||
Returns:
|
||||
bytes: Raw file contents.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
resp = await self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/read",
|
||||
json={"path": path},
|
||||
)
|
||||
_raise_for_status(resp)
|
||||
return resp.content
|
||||
|
||||
async def write(self, path: str, data: str | bytes) -> None:
|
||||
"""Write data to a file inside the capsule.
|
||||
|
||||
Creates parent directories if they do not exist.
|
||||
|
||||
Args:
|
||||
path (str): Absolute destination path inside the capsule.
|
||||
data (str | bytes): Content to write. Strings are UTF-8 encoded.
|
||||
"""
|
||||
if isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
resp = await self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/write",
|
||||
files={"file": ("upload", data)},
|
||||
data={"path": path},
|
||||
)
|
||||
_raise_for_status(resp)
|
||||
|
||||
async def list(self, path: str, depth: int = 1) -> list[FileEntry]:
|
||||
"""List directory contents.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the directory inside the capsule.
|
||||
depth (int): Recursion depth. ``1`` lists only immediate children.
|
||||
Defaults to ``1``.
|
||||
|
||||
Returns:
|
||||
list[FileEntry]: Entries in the directory.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
resp = await self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/list",
|
||||
json={"path": path, "depth": depth},
|
||||
)
|
||||
parsed = ListDirResponse.model_validate(handle_response(resp))
|
||||
return parsed.entries or []
|
||||
|
||||
async def exists(self, path: str) -> bool:
|
||||
"""Check whether a path exists inside the capsule.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to check.
|
||||
|
||||
Returns:
|
||||
bool: ``True`` if the path exists.
|
||||
"""
|
||||
parent = os.path.dirname(path)
|
||||
name = os.path.basename(path)
|
||||
try:
|
||||
entries = await self.list(parent, depth=1)
|
||||
except WrennNotFoundError:
|
||||
return False
|
||||
return any(e.name == name for e in entries)
|
||||
|
||||
async def make_dir(self, path: str) -> FileEntry:
|
||||
"""Create a directory (with parents). Idempotent.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path of the directory to create.
|
||||
|
||||
Returns:
|
||||
FileEntry: The created (or already-existing) directory entry.
|
||||
"""
|
||||
resp = await self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/mkdir",
|
||||
json={"path": path},
|
||||
)
|
||||
if _is_already_exists(resp):
|
||||
existing = await _async_find_entry(self.list, path)
|
||||
if existing is not None:
|
||||
return existing
|
||||
parsed = MakeDirResponse.model_validate(handle_response(resp))
|
||||
if parsed.entry is None:
|
||||
raise RuntimeError("mkdir response missing entry")
|
||||
return parsed.entry
|
||||
|
||||
async def remove(self, path: str) -> None:
|
||||
"""Remove a file or directory recursively.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to remove.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
resp = await self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/remove",
|
||||
json={"path": path},
|
||||
)
|
||||
handle_response(resp)
|
||||
|
||||
async def upload_stream(self, path: str, stream: AsyncIterator[bytes]) -> None:
|
||||
"""Stream a large file into the capsule.
|
||||
|
||||
Prefer this over :meth:`write` when the file is too large to hold in
|
||||
memory.
|
||||
|
||||
Args:
|
||||
path (str): Absolute destination path inside the capsule.
|
||||
stream (AsyncIterator[bytes]): Async iterable of byte chunks to
|
||||
upload.
|
||||
"""
|
||||
boundary = os.urandom(16).hex().encode("utf-8")
|
||||
preamble, trailer = _multipart_frame(path, boundary)
|
||||
|
||||
async def _multipart() -> AsyncIterator[bytes]:
|
||||
yield preamble
|
||||
async for chunk in stream:
|
||||
yield chunk if isinstance(chunk, bytes) else chunk.encode("utf-8")
|
||||
yield trailer
|
||||
|
||||
resp = await self._http.post(
|
||||
f"/v1/capsules/{self._capsule_id}/files/stream/write",
|
||||
content=_multipart(),
|
||||
headers=_multipart_headers(boundary),
|
||||
)
|
||||
_raise_for_status(resp)
|
||||
|
||||
async def download_stream(self, path: str) -> AsyncIterator[bytes]:
|
||||
"""Stream a large file out of the capsule.
|
||||
|
||||
Prefer this over :meth:`read_bytes` when the file is too large to hold
|
||||
in memory.
|
||||
|
||||
Args:
|
||||
path (str): Absolute path to the file inside the capsule.
|
||||
|
||||
Yields:
|
||||
bytes: Successive byte chunks of the file.
|
||||
|
||||
Raises:
|
||||
WrennNotFoundError: If the path does not exist.
|
||||
"""
|
||||
async with self._http.stream(
|
||||
"POST",
|
||||
f"/v1/capsules/{self._capsule_id}/files/stream/read",
|
||||
json={"path": path},
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
async for chunk in resp.aiter_bytes():
|
||||
yield chunk
|
||||
17
src/wrenn/models/__init__.py
Normal file
17
src/wrenn/models/__init__.py
Normal file
@ -0,0 +1,17 @@
|
||||
from wrenn.models._generated import (
|
||||
Capsule,
|
||||
FileEntry,
|
||||
ListDirResponse,
|
||||
MakeDirResponse,
|
||||
Status,
|
||||
Template,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"Capsule",
|
||||
"FileEntry",
|
||||
"ListDirResponse",
|
||||
"MakeDirResponse",
|
||||
"Status",
|
||||
"Template",
|
||||
]
|
||||
109
src/wrenn/models/_generated.py
Normal file
109
src/wrenn/models/_generated.py
Normal file
@ -0,0 +1,109 @@
|
||||
# generated by datamodel-codegen:
|
||||
# filename: openapi.yaml
|
||||
# timestamp: 2026-05-23T11:20:02+00:00
|
||||
|
||||
from __future__ import annotations
|
||||
from pydantic import AwareDatetime, BaseModel, Field
|
||||
from typing import Annotated
|
||||
from enum import StrEnum
|
||||
|
||||
|
||||
class Status(StrEnum):
|
||||
pending = "pending"
|
||||
starting = "starting"
|
||||
running = "running"
|
||||
pausing = "pausing"
|
||||
paused = "paused"
|
||||
snapshotting = "snapshotting"
|
||||
resuming = "resuming"
|
||||
stopping = "stopping"
|
||||
hibernated = "hibernated"
|
||||
stopped = "stopped"
|
||||
missing = "missing"
|
||||
error = "error"
|
||||
|
||||
|
||||
class Capsule(BaseModel):
|
||||
id: str | None = None
|
||||
status: Status | None = None
|
||||
template: str | None = None
|
||||
vcpus: int | None = None
|
||||
memory_mb: int | None = None
|
||||
timeout_sec: int | None = None
|
||||
created_at: AwareDatetime | None = None
|
||||
started_at: AwareDatetime | None = None
|
||||
last_active_at: AwareDatetime | None = None
|
||||
last_updated: AwareDatetime | None = None
|
||||
metadata: Annotated[
|
||||
dict[str, str] | None,
|
||||
Field(
|
||||
description="Free-form key/value labels attached at create-time. Also carries\nagent-side version info (kernel_version, vmm_version,\nagent_version, envd_version) when running.\n"
|
||||
),
|
||||
] = None
|
||||
disk_size_mb: Annotated[
|
||||
int | None, Field(description="Maximum disk capacity in MiB.")
|
||||
] = None
|
||||
disk_used_mb: Annotated[
|
||||
int | None,
|
||||
Field(
|
||||
description="Current disk usage in MiB. Only populated on individual capsule GET; omitted in list responses."
|
||||
),
|
||||
] = None
|
||||
|
||||
|
||||
class Type(StrEnum):
|
||||
base = "base"
|
||||
snapshot = "snapshot"
|
||||
|
||||
|
||||
class Template(BaseModel):
|
||||
name: str | None = None
|
||||
type: Type | None = None
|
||||
vcpus: int | None = None
|
||||
memory_mb: int | None = None
|
||||
size_bytes: int | None = None
|
||||
created_at: AwareDatetime | None = None
|
||||
platform: Annotated[
|
||||
bool | None,
|
||||
Field(
|
||||
description="True when the template is platform-managed (visible to all teams,\ne.g. the built-in `minimal-ubuntu` rootfs). False for team-owned\nsnapshot templates.\n"
|
||||
),
|
||||
] = None
|
||||
protected: Annotated[
|
||||
bool | None,
|
||||
Field(
|
||||
description="True for built-in system base templates (minimal-ubuntu,\nminimal-alpine, minimal-arch, minimal-fedora). Protected templates\ncannot be deleted.\n"
|
||||
),
|
||||
] = None
|
||||
metadata: dict[str, str] | None = None
|
||||
|
||||
|
||||
class Type2(StrEnum):
|
||||
file = "file"
|
||||
directory = "directory"
|
||||
symlink = "symlink"
|
||||
|
||||
|
||||
class FileEntry(BaseModel):
|
||||
name: str | None = None
|
||||
path: str | None = None
|
||||
type: Type2 | None = None
|
||||
size: int | None = None
|
||||
mode: int | None = None
|
||||
permissions: Annotated[
|
||||
str | None, Field(description='Human-readable permissions (e.g. "-rwxr-xr-x")')
|
||||
] = None
|
||||
owner: str | None = None
|
||||
group: str | None = None
|
||||
modified_at: Annotated[
|
||||
int | None, Field(description="Unix timestamp (seconds)")
|
||||
] = None
|
||||
symlink_target: str | None = None
|
||||
|
||||
|
||||
class MakeDirResponse(BaseModel):
|
||||
entry: FileEntry | None = None
|
||||
|
||||
|
||||
class ListDirResponse(BaseModel):
|
||||
entries: list[FileEntry] | None = None
|
||||
339
src/wrenn/pty.py
Normal file
339
src/wrenn/pty.py
Normal file
@ -0,0 +1,339 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
from collections.abc import AsyncIterator, Iterator
|
||||
from enum import StrEnum
|
||||
from typing import Any
|
||||
|
||||
import httpx_ws
|
||||
from pydantic import BaseModel
|
||||
|
||||
# A clean (``WebSocketDisconnect``) or abrupt (``WebSocketNetworkError``) close
|
||||
# both mean the PTY stream has ended; iteration must stop on either.
|
||||
_WS_CLOSED = (httpx_ws.WebSocketDisconnect, httpx_ws.WebSocketNetworkError)
|
||||
|
||||
|
||||
class PtyEventType(StrEnum):
|
||||
started = "started"
|
||||
output = "output"
|
||||
exit = "exit"
|
||||
error = "error"
|
||||
ping = "ping"
|
||||
|
||||
|
||||
class PtyEvent(BaseModel):
|
||||
type: PtyEventType
|
||||
pid: int | None = None
|
||||
tag: str | None = None
|
||||
data: bytes | str | None = None
|
||||
exit_code: int | None = None
|
||||
fatal: bool | None = None
|
||||
|
||||
|
||||
def _parse_pty_event(raw: dict[str, Any]) -> PtyEvent:
|
||||
msg_type = raw.get("type", "")
|
||||
if msg_type == "started":
|
||||
return PtyEvent(
|
||||
type=PtyEventType.started,
|
||||
pid=raw.get("pid"),
|
||||
tag=raw.get("tag"),
|
||||
)
|
||||
if msg_type == "output":
|
||||
raw_data = raw.get("data", "")
|
||||
decoded = base64.b64decode(raw_data) if raw_data else b""
|
||||
return PtyEvent(type=PtyEventType.output, data=decoded)
|
||||
if msg_type == "exit":
|
||||
return PtyEvent(type=PtyEventType.exit, exit_code=raw.get("exit_code", -1))
|
||||
if msg_type == "error":
|
||||
return PtyEvent(
|
||||
type=PtyEventType.error,
|
||||
data=raw.get("data", ""),
|
||||
fatal=raw.get("fatal", False),
|
||||
)
|
||||
if msg_type == "ping":
|
||||
return PtyEvent(type=PtyEventType.ping)
|
||||
if not msg_type:
|
||||
return PtyEvent(type=PtyEventType.ping)
|
||||
try:
|
||||
return PtyEvent(type=PtyEventType(msg_type))
|
||||
except ValueError:
|
||||
return PtyEvent(
|
||||
type=PtyEventType.error,
|
||||
data=f"unknown msg_type: {msg_type!r}",
|
||||
fatal=False,
|
||||
)
|
||||
|
||||
|
||||
class PtySession:
|
||||
"""Interactive PTY session backed by a WebSocket.
|
||||
|
||||
Use as a context manager and iterate over events::
|
||||
|
||||
with sb.pty(cmd="/bin/bash") as term:
|
||||
term.write(b"ls -la\\n")
|
||||
for event in term:
|
||||
if event.type == "output":
|
||||
sys.stdout.buffer.write(event.data)
|
||||
elif event.type == "exit":
|
||||
break
|
||||
"""
|
||||
|
||||
def __init__(self, ws: httpx_ws.WebSocketSession, capsule_id: str) -> None:
|
||||
self._ws = ws
|
||||
self._capsule_id = capsule_id
|
||||
self._tag: str | None = None
|
||||
self._pid: int | None = None
|
||||
self._done = False
|
||||
|
||||
@property
|
||||
def tag(self) -> str | None:
|
||||
"""Session tag. Available after the ``started`` event."""
|
||||
return self._tag
|
||||
|
||||
@property
|
||||
def pid(self) -> int | None:
|
||||
"""Process PID. Available after the ``started`` event."""
|
||||
return self._pid
|
||||
|
||||
def _send_start(
|
||||
self,
|
||||
cmd: str = "/bin/bash",
|
||||
args: list[str] | None = None,
|
||||
cols: int = 80,
|
||||
rows: int = 24,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
) -> None:
|
||||
msg: dict[str, Any] = {
|
||||
"type": "start",
|
||||
"cmd": cmd,
|
||||
"cols": cols or 80,
|
||||
"rows": rows or 24,
|
||||
}
|
||||
if args:
|
||||
msg["args"] = args
|
||||
if envs:
|
||||
msg["envs"] = envs
|
||||
if cwd:
|
||||
msg["cwd"] = cwd
|
||||
self._ws.send_text(json.dumps(msg))
|
||||
|
||||
def _send_connect(self, tag: str) -> None:
|
||||
self._ws.send_text(json.dumps({"type": "connect", "tag": tag}))
|
||||
|
||||
def _send_pong(self) -> None:
|
||||
"""Reply to a server keepalive ``ping`` so the session stays open."""
|
||||
try:
|
||||
self._ws.send_text(json.dumps({"type": "pong"}))
|
||||
except _WS_CLOSED:
|
||||
pass
|
||||
|
||||
def write(self, data: bytes) -> None:
|
||||
"""Send raw bytes to the PTY stdin.
|
||||
|
||||
Args:
|
||||
data: Raw bytes to send. Base64-encoded internally.
|
||||
"""
|
||||
encoded = base64.b64encode(data).decode("ascii")
|
||||
self._ws.send_text(json.dumps({"type": "input", "data": encoded}))
|
||||
|
||||
def resize(self, cols: int, rows: int) -> None:
|
||||
"""Resize the PTY terminal.
|
||||
|
||||
Args:
|
||||
cols: New column count. Must be > 0.
|
||||
rows: New row count. Must be > 0.
|
||||
|
||||
Raises:
|
||||
ValueError: If cols or rows is 0.
|
||||
"""
|
||||
if cols <= 0 or rows <= 0:
|
||||
raise ValueError("cols and rows must be greater than 0")
|
||||
self._ws.send_text(json.dumps({"type": "resize", "cols": cols, "rows": rows}))
|
||||
|
||||
def kill(self) -> None:
|
||||
"""Send SIGKILL to the PTY process."""
|
||||
self._ws.send_text(json.dumps({"type": "kill"}))
|
||||
|
||||
def __iter__(self) -> Iterator[PtyEvent]:
|
||||
return self
|
||||
|
||||
def __next__(self) -> PtyEvent:
|
||||
if self._done:
|
||||
raise StopIteration
|
||||
try:
|
||||
raw = self._ws.receive_text()
|
||||
except _WS_CLOSED:
|
||||
raise StopIteration
|
||||
event = _parse_pty_event(json.loads(raw))
|
||||
if event.type == PtyEventType.started:
|
||||
if event.tag is not None:
|
||||
self._tag = event.tag
|
||||
if event.pid is not None:
|
||||
self._pid = event.pid
|
||||
if event.type == PtyEventType.ping:
|
||||
self._send_pong()
|
||||
if event.type == PtyEventType.exit:
|
||||
self._done = True
|
||||
return event
|
||||
if event.type == PtyEventType.error and event.fatal:
|
||||
self._done = True
|
||||
return event
|
||||
return event
|
||||
|
||||
def __enter__(self) -> PtySession:
|
||||
return self
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: object,
|
||||
) -> None:
|
||||
try:
|
||||
self.kill()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
self._ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
class AsyncPtySession:
|
||||
"""Async interactive PTY session backed by a WebSocket.
|
||||
|
||||
Use as an async context manager and async iterate over events::
|
||||
|
||||
async with sb.pty(cmd="/bin/bash") as term:
|
||||
await term.write(b"ls -la\\n")
|
||||
async for event in term:
|
||||
if event.type == "output":
|
||||
sys.stdout.buffer.write(event.data)
|
||||
elif event.type == "exit":
|
||||
break
|
||||
"""
|
||||
|
||||
def __init__(self, ws: httpx_ws.AsyncWebSocketSession, capsule_id: str) -> None:
|
||||
self._ws = ws
|
||||
self._capsule_id = capsule_id
|
||||
self._tag: str | None = None
|
||||
self._pid: int | None = None
|
||||
self._done = False
|
||||
|
||||
@property
|
||||
def tag(self) -> str | None:
|
||||
"""Session tag. Available after the ``started`` event."""
|
||||
return self._tag
|
||||
|
||||
@property
|
||||
def pid(self) -> int | None:
|
||||
"""Process PID. Available after the ``started`` event."""
|
||||
return self._pid
|
||||
|
||||
async def _send_start(
|
||||
self,
|
||||
cmd: str = "/bin/bash",
|
||||
args: list[str] | None = None,
|
||||
cols: int = 80,
|
||||
rows: int = 24,
|
||||
envs: dict[str, str] | None = None,
|
||||
cwd: str | None = None,
|
||||
) -> None:
|
||||
msg: dict[str, Any] = {
|
||||
"type": "start",
|
||||
"cmd": cmd,
|
||||
"cols": cols or 80,
|
||||
"rows": rows or 24,
|
||||
}
|
||||
if args:
|
||||
msg["args"] = args
|
||||
if envs:
|
||||
msg["envs"] = envs
|
||||
if cwd:
|
||||
msg["cwd"] = cwd
|
||||
await self._ws.send_text(json.dumps(msg))
|
||||
|
||||
async def _send_connect(self, tag: str) -> None:
|
||||
await self._ws.send_text(json.dumps({"type": "connect", "tag": tag}))
|
||||
|
||||
async def _send_pong(self) -> None:
|
||||
"""Reply to a server keepalive ``ping`` so the session stays open."""
|
||||
try:
|
||||
await self._ws.send_text(json.dumps({"type": "pong"}))
|
||||
except _WS_CLOSED:
|
||||
pass
|
||||
|
||||
async def write(self, data: bytes) -> None:
|
||||
"""Send raw bytes to the PTY stdin.
|
||||
|
||||
Args:
|
||||
data: Raw bytes to send. Base64-encoded internally.
|
||||
"""
|
||||
encoded = base64.b64encode(data).decode("ascii")
|
||||
await self._ws.send_text(json.dumps({"type": "input", "data": encoded}))
|
||||
|
||||
async def resize(self, cols: int, rows: int) -> None:
|
||||
"""Resize the PTY terminal.
|
||||
|
||||
Args:
|
||||
cols: New column count. Must be > 0.
|
||||
rows: New row count. Must be > 0.
|
||||
|
||||
Raises:
|
||||
ValueError: If cols or rows is 0.
|
||||
"""
|
||||
if cols <= 0 or rows <= 0:
|
||||
raise ValueError("cols and rows must be greater than 0")
|
||||
await self._ws.send_text(
|
||||
json.dumps({"type": "resize", "cols": cols, "rows": rows})
|
||||
)
|
||||
|
||||
async def kill(self) -> None:
|
||||
"""Send SIGKILL to the PTY process."""
|
||||
await self._ws.send_text(json.dumps({"type": "kill"}))
|
||||
|
||||
def __aiter__(self) -> AsyncIterator[PtyEvent]:
|
||||
return self
|
||||
|
||||
async def __anext__(self) -> PtyEvent:
|
||||
if self._done:
|
||||
raise StopAsyncIteration
|
||||
try:
|
||||
raw = await self._ws.receive_text()
|
||||
except _WS_CLOSED:
|
||||
raise StopAsyncIteration
|
||||
event = _parse_pty_event(json.loads(raw))
|
||||
if event.type == PtyEventType.started:
|
||||
if event.tag is not None:
|
||||
self._tag = event.tag
|
||||
if event.pid is not None:
|
||||
self._pid = event.pid
|
||||
if event.type == PtyEventType.ping:
|
||||
await self._send_pong()
|
||||
if event.type == PtyEventType.exit:
|
||||
self._done = True
|
||||
return event
|
||||
if event.type == PtyEventType.error and event.fatal:
|
||||
self._done = True
|
||||
return event
|
||||
return event
|
||||
|
||||
async def __aenter__(self) -> AsyncPtySession:
|
||||
return self
|
||||
|
||||
async def __aexit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: object,
|
||||
) -> None:
|
||||
try:
|
||||
await self.kill()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
await self._ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
22
src/wrenn/sandbox.py
Normal file
22
src/wrenn/sandbox.py
Normal file
@ -0,0 +1,22 @@
|
||||
import warnings as _warnings
|
||||
|
||||
from wrenn.capsule import Capsule # noqa: F401
|
||||
from wrenn.commands import ( # noqa: F401
|
||||
StreamErrorEvent,
|
||||
StreamEvent,
|
||||
StreamExitEvent,
|
||||
StreamStartEvent,
|
||||
StreamStderrEvent,
|
||||
StreamStdoutEvent,
|
||||
)
|
||||
|
||||
|
||||
def __getattr__(name: str) -> type:
|
||||
if name == "Sandbox":
|
||||
_warnings.warn(
|
||||
"'Sandbox' is deprecated, use 'Capsule' instead",
|
||||
FutureWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return Capsule
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
37
tests/conftest.py
Normal file
37
tests/conftest.py
Normal file
@ -0,0 +1,37 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ENV_FILE = Path(__file__).resolve().parent.parent / ".env"
|
||||
|
||||
|
||||
def _read_env_file() -> dict[str, str]:
|
||||
result: dict[str, str] = {}
|
||||
if not ENV_FILE.exists():
|
||||
return result
|
||||
for line in ENV_FILE.read_text().splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, _, value = line.partition("=")
|
||||
key = key.strip()
|
||||
value = value.strip().strip("\"'")
|
||||
if key:
|
||||
result[key] = value
|
||||
return result
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(
|
||||
config: pytest.Config, items: list[pytest.Item]
|
||||
) -> None:
|
||||
env_vars = _read_env_file()
|
||||
has_key = bool(os.environ.get("WRENN_API_KEY") or env_vars.get("WRENN_API_KEY"))
|
||||
if has_key:
|
||||
return
|
||||
skip = pytest.mark.skip(reason="WRENN_API_KEY not set")
|
||||
for item in items:
|
||||
if "integration" in item.keywords:
|
||||
item.add_marker(skip)
|
||||
425
tests/test_capsule_features.py
Normal file
425
tests/test_capsule_features.py
Normal file
@ -0,0 +1,425 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from wrenn.capsule import Capsule, _build_http_proxy_url, _build_proxy_url
|
||||
from wrenn.code_runner.models import Execution, ExecutionError, Logs, Result
|
||||
|
||||
BASE = "https://app.wrenn.dev/api"
|
||||
API_KEY = "wrn_test1234567890abcdef12345678"
|
||||
|
||||
|
||||
class TestBuildProxyUrl:
|
||||
def test_https_production(self):
|
||||
url = _build_proxy_url("https://app.wrenn.dev/api", "cl-abc123", 8888)
|
||||
assert url == "wss://8888-cl-abc123.app.wrenn.dev"
|
||||
|
||||
def test_http_localhost(self):
|
||||
url = _build_proxy_url("http://localhost:8080", "cl-abc123", 3000)
|
||||
assert url == "ws://3000-cl-abc123.localhost:8080"
|
||||
|
||||
def test_https_custom_port(self):
|
||||
url = _build_proxy_url("https://api.example.com:9443", "sb-1", 8080)
|
||||
assert url == "wss://8080-sb-1.api.example.com:9443"
|
||||
|
||||
def test_http_no_port(self):
|
||||
url = _build_proxy_url("http://192.168.1.1", "sb-2", 5000)
|
||||
assert url == "ws://5000-sb-2.192.168.1.1"
|
||||
|
||||
|
||||
class TestBuildHttpProxyUrl:
|
||||
"""``get_url`` returns an HTTP(S) URL; ``/api`` path on the base URL is
|
||||
discarded — only the host is used to build the proxy subdomain."""
|
||||
|
||||
def test_https_production_strips_api_path(self):
|
||||
url = _build_http_proxy_url("https://app.wrenn.dev/api", "cl-abc", 8080)
|
||||
assert url == "https://8080-cl-abc.app.wrenn.dev"
|
||||
|
||||
def test_http_localhost_preserves_port(self):
|
||||
url = _build_http_proxy_url("http://localhost:8080/api", "cl-abc", 3000)
|
||||
assert url == "http://3000-cl-abc.localhost:8080"
|
||||
|
||||
def test_https_custom_port(self):
|
||||
url = _build_http_proxy_url("https://api.example.com:9443", "sb-1", 80)
|
||||
assert url == "https://80-sb-1.api.example.com:9443"
|
||||
|
||||
def test_proxy_domain_override_http(self):
|
||||
url = _build_http_proxy_url(
|
||||
"https://app.wrenn.dev/api", "cl-abc", 8080, "wrenn.dev"
|
||||
)
|
||||
assert url == "https://8080-cl-abc.wrenn.dev"
|
||||
|
||||
def test_proxy_domain_override_ws(self):
|
||||
url = _build_proxy_url("https://app.wrenn.dev/api", "cl-abc", 8888, "wrenn.dev")
|
||||
assert url == "wss://8888-cl-abc.wrenn.dev"
|
||||
|
||||
|
||||
class TestCapsuleCreate:
|
||||
@respx.mock
|
||||
def test_capsule_constructor_creates(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting", "template": "minimal"}
|
||||
)
|
||||
cap = Capsule(
|
||||
template="minimal",
|
||||
api_key="wrn_test1234567890abcdef12345678",
|
||||
base_url=BASE,
|
||||
)
|
||||
assert cap.capsule_id == "cl-1"
|
||||
assert hasattr(cap, "commands")
|
||||
assert hasattr(cap, "files")
|
||||
|
||||
@respx.mock
|
||||
def test_capsule_create_classmethod(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-2", "status": "starting"}
|
||||
)
|
||||
cap = Capsule.create(api_key="wrn_test1234567890abcdef12345678", base_url=BASE)
|
||||
assert cap.capsule_id == "cl-2"
|
||||
|
||||
@respx.mock
|
||||
def test_capsule_context_manager_kills(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting"}
|
||||
)
|
||||
kill_route = respx.delete(f"{BASE}/v1/capsules/cl-1").respond(202)
|
||||
with Capsule(api_key="wrn_test1234567890abcdef12345678", base_url=BASE) as cap:
|
||||
assert cap.capsule_id == "cl-1"
|
||||
assert kill_route.called
|
||||
|
||||
@respx.mock
|
||||
def test_capsule_env_var(self, monkeypatch):
|
||||
monkeypatch.setenv("WRENN_API_KEY", "wrn_from_env_key")
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-3", "status": "starting"}
|
||||
)
|
||||
cap = Capsule(base_url=BASE)
|
||||
assert cap.capsule_id == "cl-3"
|
||||
|
||||
|
||||
class TestCapsuleStaticMethods:
|
||||
@respx.mock
|
||||
def test_static_destroy(self):
|
||||
route = respx.delete(f"{BASE}/v1/capsules/cl-1").respond(202)
|
||||
Capsule._static_destroy(
|
||||
"cl-1", api_key="wrn_test1234567890abcdef12345678", base_url=BASE
|
||||
)
|
||||
assert route.called
|
||||
|
||||
@respx.mock
|
||||
def test_static_pause(self):
|
||||
respx.post(f"{BASE}/v1/capsules/cl-1/pause").respond(
|
||||
202, json={"id": "cl-1", "status": "pausing"}
|
||||
)
|
||||
info = Capsule._static_pause(
|
||||
"cl-1", api_key="wrn_test1234567890abcdef12345678", base_url=BASE
|
||||
)
|
||||
assert info.status.value == "pausing"
|
||||
|
||||
@respx.mock
|
||||
def test_static_list(self):
|
||||
respx.get(f"{BASE}/v1/capsules").respond(
|
||||
200, json=[{"id": "cl-1", "status": "running"}]
|
||||
)
|
||||
items = Capsule.list(api_key="wrn_test1234567890abcdef12345678", base_url=BASE)
|
||||
assert len(items) == 1
|
||||
assert items[0].id == "cl-1"
|
||||
|
||||
@respx.mock
|
||||
def test_static_get_info(self):
|
||||
respx.get(f"{BASE}/v1/capsules/cl-1").respond(
|
||||
200, json={"id": "cl-1", "status": "running"}
|
||||
)
|
||||
info = Capsule._static_get_info(
|
||||
"cl-1", api_key="wrn_test1234567890abcdef12345678", base_url=BASE
|
||||
)
|
||||
assert info.id == "cl-1"
|
||||
|
||||
|
||||
class TestCapsuleConnect:
|
||||
@respx.mock
|
||||
def test_connect_running(self):
|
||||
respx.get(f"{BASE}/v1/capsules/cl-1").respond(
|
||||
200, json={"id": "cl-1", "status": "running"}
|
||||
)
|
||||
cap = Capsule.connect(
|
||||
"cl-1", api_key="wrn_test1234567890abcdef12345678", base_url=BASE
|
||||
)
|
||||
assert cap.capsule_id == "cl-1"
|
||||
|
||||
@respx.mock
|
||||
def test_connect_paused_resumes(self):
|
||||
get_route = respx.get(f"{BASE}/v1/capsules/cl-1")
|
||||
get_route.side_effect = [
|
||||
httpx.Response(200, json={"id": "cl-1", "status": "paused"}),
|
||||
httpx.Response(200, json={"id": "cl-1", "status": "running"}),
|
||||
]
|
||||
respx.post(f"{BASE}/v1/capsules/cl-1/resume").respond(
|
||||
202, json={"id": "cl-1", "status": "resuming"}
|
||||
)
|
||||
cap = Capsule.connect(
|
||||
"cl-1", api_key="wrn_test1234567890abcdef12345678", base_url=BASE
|
||||
)
|
||||
assert cap.capsule_id == "cl-1"
|
||||
|
||||
|
||||
class TestExecutionModels:
|
||||
def test_execution_defaults(self):
|
||||
e = Execution()
|
||||
assert e.results == []
|
||||
assert e.logs.stdout == []
|
||||
assert e.logs.stderr == []
|
||||
assert e.error is None
|
||||
assert e.text is None
|
||||
|
||||
def test_result_from_bundle(self):
|
||||
bundle = {"text/plain": "84", "image/png": "base64data"}
|
||||
r = Result.from_bundle(bundle, is_main_result=True)
|
||||
assert r.text == "84"
|
||||
assert r.png == "base64data"
|
||||
assert r.is_main_result is True
|
||||
|
||||
def test_result_from_bundle_preserves_text_plain(self):
|
||||
# ``text/plain`` is the Jupyter repr — preserved verbatim now.
|
||||
bundle = {"text/plain": "'hello'"}
|
||||
r = Result.from_bundle(bundle)
|
||||
assert r.text == "'hello'"
|
||||
|
||||
def test_result_from_bundle_extra_mimes(self):
|
||||
bundle = {"text/plain": "x", "application/vnd.custom": "data"}
|
||||
r = Result.from_bundle(bundle)
|
||||
assert r.extra == {"application/vnd.custom": "data"}
|
||||
|
||||
def test_result_formats(self):
|
||||
r = Result(text="hi", png="data")
|
||||
assert "text" in r.formats()
|
||||
assert "png" in r.formats()
|
||||
assert "html" not in r.formats()
|
||||
|
||||
def test_execution_text_property(self):
|
||||
e = Execution(
|
||||
results=[
|
||||
Result(text="chart", is_main_result=False),
|
||||
Result(text="42", is_main_result=True),
|
||||
]
|
||||
)
|
||||
assert e.text == "42"
|
||||
|
||||
def test_execution_error(self):
|
||||
err = ExecutionError(
|
||||
name="ZeroDivisionError",
|
||||
value="division by zero",
|
||||
traceback="Traceback ...\nZeroDivisionError: division by zero",
|
||||
)
|
||||
e = Execution(error=err)
|
||||
assert e.error is not None
|
||||
assert "ZeroDivisionError" in e.error.name
|
||||
|
||||
def test_logs(self):
|
||||
logs = Logs(stdout=["hello\n", "world\n"], stderr=["warn\n"])
|
||||
assert "".join(logs.stdout) == "hello\nworld\n"
|
||||
assert "".join(logs.stderr) == "warn\n"
|
||||
|
||||
|
||||
class TestGetUrlPublic:
|
||||
"""``Capsule.get_url`` returns the HTTP proxy URL."""
|
||||
|
||||
@respx.mock
|
||||
def test_sync_get_url_default_base(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-99", "status": "starting"}
|
||||
)
|
||||
cap = Capsule(api_key=API_KEY, base_url=BASE)
|
||||
assert cap.get_url(8080) == "https://8080-cl-99.wrenn.dev"
|
||||
|
||||
@respx.mock
|
||||
def test_sync_get_url_localhost(self):
|
||||
local_base = "http://localhost:8080/api"
|
||||
respx.post(f"{local_base}/v1/capsules").respond(
|
||||
202, json={"id": "cl-42", "status": "starting"}
|
||||
)
|
||||
cap = Capsule(api_key=API_KEY, base_url=local_base)
|
||||
assert cap.get_url(3000) == "http://3000-cl-42.localhost:8080"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_get_url(self):
|
||||
from wrenn.async_capsule import AsyncCapsule
|
||||
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-async", "status": "starting"}
|
||||
)
|
||||
cap = await AsyncCapsule.create(api_key=API_KEY, base_url=BASE)
|
||||
assert cap.get_url(5000) == "https://5000-cl-async.wrenn.dev"
|
||||
await cap._client.aclose()
|
||||
|
||||
|
||||
class TestPtyConnect:
|
||||
"""``pty_connect`` reconnects to an existing PTY session by tag."""
|
||||
|
||||
def _capsule(self):
|
||||
with respx.mock:
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting"}
|
||||
)
|
||||
return Capsule(api_key=API_KEY, base_url=BASE)
|
||||
|
||||
def test_sync_pty_connect_sends_connect_frame(self):
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
cap = self._capsule()
|
||||
ws = MagicMock()
|
||||
ctx = MagicMock()
|
||||
ctx.__enter__.return_value = ws
|
||||
ctx.__exit__.return_value = False
|
||||
|
||||
with patch("wrenn.capsule.httpx_ws.connect_ws", return_value=ctx):
|
||||
with cap.pty_connect("tag-xyz") as session:
|
||||
assert session is not None
|
||||
# First send_text call must be a ``connect`` frame with the tag.
|
||||
import json as _json
|
||||
|
||||
sent = ws.send_text.call_args_list[0].args[0]
|
||||
payload = _json.loads(sent)
|
||||
assert payload == {"type": "connect", "tag": "tag-xyz"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_pty_connect_sends_connect_frame(self):
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from wrenn.async_capsule import AsyncCapsule
|
||||
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting"}
|
||||
)
|
||||
cap = await AsyncCapsule.create(api_key=API_KEY, base_url=BASE)
|
||||
ws = MagicMock()
|
||||
ws.send_text = AsyncMock()
|
||||
ctx = MagicMock()
|
||||
ctx.__aenter__ = AsyncMock(return_value=ws)
|
||||
ctx.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch("wrenn.async_capsule.httpx_ws.aconnect_ws", return_value=ctx):
|
||||
async with cap.pty_connect("tag-async") as session:
|
||||
assert session is not None
|
||||
import json as _json
|
||||
|
||||
sent = ws.send_text.call_args_list[0].args[0]
|
||||
payload = _json.loads(sent)
|
||||
assert payload == {"type": "connect", "tag": "tag-async"}
|
||||
await cap._client.aclose()
|
||||
|
||||
|
||||
class TestCreateSnapshot:
|
||||
@respx.mock
|
||||
def test_sync_create_snapshot_posts_capsule_id(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting"}
|
||||
)
|
||||
snap_route = respx.post(f"{BASE}/v1/snapshots").respond(
|
||||
201,
|
||||
json={"name": "my-snap"},
|
||||
)
|
||||
cap = Capsule(api_key=API_KEY, base_url=BASE)
|
||||
tpl = cap.create_snapshot(name="my-snap", overwrite=True)
|
||||
import json as _json
|
||||
|
||||
req = snap_route.calls[0].request
|
||||
body = _json.loads(req.content)
|
||||
assert body["sandbox_id"] == "cl-1"
|
||||
assert body["name"] == "my-snap"
|
||||
assert req.url.params["overwrite"] == "true"
|
||||
assert tpl.name == "my-snap"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_create_snapshot(self):
|
||||
from wrenn.async_capsule import AsyncCapsule
|
||||
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting"}
|
||||
)
|
||||
respx.post(f"{BASE}/v1/snapshots").respond(
|
||||
201,
|
||||
json={"name": "auto-named"},
|
||||
)
|
||||
cap = await AsyncCapsule.create(api_key=API_KEY, base_url=BASE)
|
||||
tpl = await cap.create_snapshot()
|
||||
assert tpl.name == "auto-named"
|
||||
await cap._client.aclose()
|
||||
|
||||
|
||||
class TestUploadStreamChunked:
|
||||
"""``upload_stream`` must declare ``Transfer-Encoding: chunked`` and
|
||||
deliver the multipart body without buffering."""
|
||||
|
||||
@respx.mock
|
||||
def test_sync_upload_stream_chunked(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting"}
|
||||
)
|
||||
route = respx.post(f"{BASE}/v1/capsules/cl-1/files/stream/write").respond(
|
||||
200, json={}
|
||||
)
|
||||
cap = Capsule(api_key=API_KEY, base_url=BASE)
|
||||
|
||||
def chunks():
|
||||
yield b"hello "
|
||||
yield b"world\n"
|
||||
|
||||
cap.files.upload_stream("/tmp/out.txt", chunks())
|
||||
req = route.calls[0].request
|
||||
assert req.headers["transfer-encoding"] == "chunked"
|
||||
ct = req.headers["content-type"]
|
||||
assert ct.startswith("multipart/form-data; boundary=")
|
||||
body = bytes(req.content)
|
||||
assert b'name="path"' in body
|
||||
assert b"/tmp/out.txt" in body
|
||||
assert b'name="file"' in body
|
||||
assert b"hello world\n" in body
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_upload_stream_chunked(self):
|
||||
from wrenn.async_capsule import AsyncCapsule
|
||||
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "cl-1", "status": "starting"}
|
||||
)
|
||||
route = respx.post(f"{BASE}/v1/capsules/cl-1/files/stream/write").respond(
|
||||
200, json={}
|
||||
)
|
||||
cap = await AsyncCapsule.create(api_key=API_KEY, base_url=BASE)
|
||||
|
||||
async def chunks():
|
||||
yield b"abc"
|
||||
yield b"def"
|
||||
|
||||
await cap.files.upload_stream("/tmp/out.bin", chunks())
|
||||
req = route.calls[0].request
|
||||
assert req.headers["transfer-encoding"] == "chunked"
|
||||
body = bytes(req.content)
|
||||
assert b"abcdef" in body
|
||||
await cap._client.aclose()
|
||||
|
||||
|
||||
class TestDeprecationWarnings:
|
||||
def test_import_sandbox_from_wrenn_warns(self):
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
# Clear cached attribute
|
||||
if "Sandbox" in dir(sys.modules.get("wrenn", object())):
|
||||
delattr(sys.modules["wrenn"], "Sandbox")
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
from wrenn import Sandbox
|
||||
|
||||
assert Sandbox is Capsule
|
||||
fw = [x for x in w if issubclass(x.category, FutureWarning)]
|
||||
assert len(fw) >= 1
|
||||
assert "Sandbox" in str(fw[0].message)
|
||||
299
tests/test_client.py
Normal file
299
tests/test_client.py
Normal file
@ -0,0 +1,299 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from wrenn.client import AsyncWrennClient, WrennClient
|
||||
from wrenn.exceptions import (
|
||||
WrennAgentError,
|
||||
WrennAuthenticationError,
|
||||
WrennConflictError,
|
||||
WrennInternalError,
|
||||
WrennNotFoundError,
|
||||
WrennValidationError,
|
||||
)
|
||||
from wrenn.models import (
|
||||
Capsule,
|
||||
Status,
|
||||
Template,
|
||||
)
|
||||
|
||||
BASE = "https://app.wrenn.dev/api"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with WrennClient(api_key="wrn_test1234567890abcdef12345678", base_url=BASE) as c:
|
||||
yield c
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def async_client():
|
||||
return AsyncWrennClient(api_key="wrn_test1234567890abcdef12345678", base_url=BASE)
|
||||
|
||||
|
||||
class TestCapsules:
|
||||
@respx.mock
|
||||
def test_create(self, client):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202,
|
||||
json={
|
||||
"id": "sb-1",
|
||||
"status": "starting",
|
||||
"template": "base-python",
|
||||
"vcpus": 2,
|
||||
"memory_mb": 1024,
|
||||
},
|
||||
)
|
||||
resp = client.capsules.create(template="base-python", vcpus=2, memory_mb=1024)
|
||||
assert isinstance(resp, Capsule)
|
||||
assert resp.id == "sb-1"
|
||||
assert resp.status == Status.starting
|
||||
|
||||
@respx.mock
|
||||
def test_create_defaults(self, client):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-2", "status": "starting"}
|
||||
)
|
||||
resp = client.capsules.create()
|
||||
assert resp.id == "sb-2"
|
||||
|
||||
@respx.mock
|
||||
def test_list(self, client):
|
||||
respx.get(f"{BASE}/v1/capsules").respond(
|
||||
200, json=[{"id": "sb-1", "status": "running"}]
|
||||
)
|
||||
boxes = client.capsules.list()
|
||||
assert len(boxes) == 1
|
||||
assert boxes[0].status == Status.running
|
||||
|
||||
@respx.mock
|
||||
def test_get(self, client):
|
||||
respx.get(f"{BASE}/v1/capsules/sb-1").respond(
|
||||
200, json={"id": "sb-1", "status": "running"}
|
||||
)
|
||||
resp = client.capsules.get("sb-1")
|
||||
assert resp.id == "sb-1"
|
||||
|
||||
@respx.mock
|
||||
def test_destroy(self, client):
|
||||
route = respx.delete(f"{BASE}/v1/capsules/sb-1").respond(202)
|
||||
client.capsules.destroy("sb-1")
|
||||
assert route.called
|
||||
|
||||
@respx.mock
|
||||
def test_pause(self, client):
|
||||
respx.post(f"{BASE}/v1/capsules/sb-1/pause").respond(
|
||||
202, json={"id": "sb-1", "status": "pausing"}
|
||||
)
|
||||
resp = client.capsules.pause("sb-1")
|
||||
assert resp.status == Status.pausing
|
||||
|
||||
@respx.mock
|
||||
def test_resume(self, client):
|
||||
respx.post(f"{BASE}/v1/capsules/sb-1/resume").respond(
|
||||
202, json={"id": "sb-1", "status": "resuming"}
|
||||
)
|
||||
resp = client.capsules.resume("sb-1")
|
||||
assert resp.status == Status.resuming
|
||||
|
||||
@respx.mock
|
||||
def test_ping(self, client):
|
||||
route = respx.post(f"{BASE}/v1/capsules/sb-1/ping").respond(204)
|
||||
client.capsules.ping("sb-1")
|
||||
assert route.called
|
||||
|
||||
|
||||
class TestSnapshots:
|
||||
@respx.mock
|
||||
def test_create(self, client):
|
||||
respx.post(f"{BASE}/v1/snapshots").respond(
|
||||
201,
|
||||
json={"name": "snap-1", "type": "snapshot", "vcpus": 1},
|
||||
)
|
||||
resp = client.snapshots.create(capsule_id="sb-1", name="snap-1")
|
||||
assert isinstance(resp, Template)
|
||||
assert resp.name == "snap-1"
|
||||
|
||||
@respx.mock
|
||||
def test_create_with_overwrite(self, client):
|
||||
route = respx.post(f"{BASE}/v1/snapshots").respond(
|
||||
201, json={"name": "snap-1", "type": "snapshot"}
|
||||
)
|
||||
client.snapshots.create(capsule_id="sb-1", overwrite=True)
|
||||
req = route.calls[0].request
|
||||
assert "overwrite=true" in str(req.url)
|
||||
|
||||
@respx.mock
|
||||
def test_list(self, client):
|
||||
respx.get(f"{BASE}/v1/snapshots").respond(
|
||||
200, json=[{"name": "base-python", "type": "base"}]
|
||||
)
|
||||
snaps = client.snapshots.list()
|
||||
assert len(snaps) == 1
|
||||
|
||||
@respx.mock
|
||||
def test_list_with_filter(self, client):
|
||||
route = respx.get(f"{BASE}/v1/snapshots").respond(200, json=[])
|
||||
client.snapshots.list(type="snapshot")
|
||||
req = route.calls[0].request
|
||||
assert "type=snapshot" in str(req.url)
|
||||
|
||||
@respx.mock
|
||||
def test_delete(self, client):
|
||||
route = respx.delete(f"{BASE}/v1/snapshots/snap-1").respond(204)
|
||||
client.snapshots.delete("snap-1")
|
||||
assert route.called
|
||||
|
||||
|
||||
class TestErrorHandling:
|
||||
@respx.mock
|
||||
def test_validation_error(self, client):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
400,
|
||||
json={"error": {"code": "invalid_request", "message": "bad input"}},
|
||||
)
|
||||
with pytest.raises(WrennValidationError) as exc_info:
|
||||
client.capsules.create()
|
||||
assert exc_info.value.code == "invalid_request"
|
||||
assert exc_info.value.status_code == 400
|
||||
|
||||
@respx.mock
|
||||
def test_auth_error(self, client):
|
||||
respx.get(f"{BASE}/v1/capsules").respond(
|
||||
401,
|
||||
json={"error": {"code": "unauthorized", "message": "bad key"}},
|
||||
)
|
||||
with pytest.raises(WrennAuthenticationError):
|
||||
client.capsules.list()
|
||||
|
||||
@respx.mock
|
||||
def test_not_found_error(self, client):
|
||||
respx.get(f"{BASE}/v1/capsules/nope").respond(
|
||||
404,
|
||||
json={"error": {"code": "not_found", "message": "capsule not found"}},
|
||||
)
|
||||
with pytest.raises(WrennNotFoundError):
|
||||
client.capsules.get("nope")
|
||||
|
||||
@respx.mock
|
||||
def test_conflict_error(self, client):
|
||||
respx.get(f"{BASE}/v1/capsules/sb-1").respond(
|
||||
409,
|
||||
json={"error": {"code": "invalid_state", "message": "not running"}},
|
||||
)
|
||||
with pytest.raises(WrennConflictError):
|
||||
client.capsules.get("sb-1")
|
||||
|
||||
@respx.mock
|
||||
def test_agent_error(self, client):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
502,
|
||||
json={"error": {"code": "agent_error", "message": "host agent failed"}},
|
||||
)
|
||||
with pytest.raises(WrennAgentError):
|
||||
client.capsules.create()
|
||||
|
||||
@respx.mock
|
||||
def test_internal_error(self, client):
|
||||
respx.get(f"{BASE}/v1/capsules/sb-1").respond(
|
||||
500,
|
||||
json={"error": {"code": "internal_error", "message": "oops"}},
|
||||
)
|
||||
with pytest.raises(WrennInternalError):
|
||||
client.capsules.get("sb-1")
|
||||
|
||||
@respx.mock
|
||||
def test_unknown_error_code_falls_back(self, client):
|
||||
respx.get(f"{BASE}/v1/capsules/sb-1").respond(
|
||||
418,
|
||||
json={"error": {"code": "teapot", "message": "I'm a teapot"}},
|
||||
)
|
||||
from wrenn.exceptions import WrennError
|
||||
|
||||
with pytest.raises(WrennError) as exc_info:
|
||||
client.capsules.get("sb-1")
|
||||
assert exc_info.value.code == "teapot"
|
||||
|
||||
|
||||
class TestAuthModes:
|
||||
def test_api_key_header(self):
|
||||
with WrennClient(api_key="wrn_test1234567890abcdef12345678") as c:
|
||||
assert c._http.headers["X-API-Key"] == "wrn_test1234567890abcdef12345678"
|
||||
|
||||
def test_no_auth_raises(self, monkeypatch):
|
||||
monkeypatch.delenv("WRENN_API_KEY", raising=False)
|
||||
with pytest.raises(ValueError, match="No API key"):
|
||||
WrennClient()
|
||||
|
||||
def test_env_var_fallback(self, monkeypatch):
|
||||
monkeypatch.setenv("WRENN_API_KEY", "wrn_from_env")
|
||||
with WrennClient() as c:
|
||||
assert c._http.headers["X-API-Key"] == "wrn_from_env"
|
||||
|
||||
|
||||
class TestAsyncClient:
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_capsules_create(self, async_client):
|
||||
async with async_client:
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-1", "status": "starting"}
|
||||
)
|
||||
resp = await async_client.capsules.create(template="base-python")
|
||||
assert resp.id == "sb-1"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_capsules_list(self, async_client):
|
||||
async with async_client:
|
||||
respx.get(f"{BASE}/v1/capsules").respond(200, json=[{"id": "sb-1"}])
|
||||
boxes = await async_client.capsules.list()
|
||||
assert len(boxes) == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_error_handling(self, async_client):
|
||||
async with async_client:
|
||||
respx.get(f"{BASE}/v1/capsules/nope").respond(
|
||||
404,
|
||||
json={"error": {"code": "not_found", "message": "not found"}},
|
||||
)
|
||||
with pytest.raises(WrennNotFoundError):
|
||||
await async_client.capsules.get("nope")
|
||||
|
||||
|
||||
class TestClientResolution:
|
||||
def test_default_base_url_strips_app_subdomain(self):
|
||||
with WrennClient(api_key="wrn_test1234567890abcdef12345678") as c:
|
||||
assert c._proxy_domain == "wrenn.dev"
|
||||
|
||||
def test_custom_base_url_preserves_host(self):
|
||||
with WrennClient(
|
||||
api_key="wrn_test1234567890abcdef12345678",
|
||||
base_url="http://localhost:8080/api",
|
||||
) as c:
|
||||
assert c._proxy_domain == "localhost:8080"
|
||||
|
||||
def test_explicit_proxy_domain_wins(self):
|
||||
with WrennClient(
|
||||
api_key="wrn_test1234567890abcdef12345678",
|
||||
base_url="https://app.wrenn.dev/api",
|
||||
proxy_domain="custom.example.com",
|
||||
) as c:
|
||||
assert c._proxy_domain == "custom.example.com"
|
||||
|
||||
def test_env_proxy_domain(self, monkeypatch):
|
||||
monkeypatch.setenv("WRENN_PROXY_DOMAIN", "env.example.com")
|
||||
with WrennClient(api_key="wrn_test1234567890abcdef12345678") as c:
|
||||
assert c._proxy_domain == "env.example.com"
|
||||
|
||||
def test_default_timeout(self):
|
||||
with WrennClient(api_key="wrn_test1234567890abcdef12345678") as c:
|
||||
t = c._http.timeout
|
||||
assert t.connect == 10.0
|
||||
assert t.read == 30.0
|
||||
|
||||
def test_timeout_float_override(self):
|
||||
with WrennClient(api_key="wrn_test1234567890abcdef12345678", timeout=5.0) as c:
|
||||
assert c._http.timeout.connect == 5.0
|
||||
521
tests/test_code_runner_e2e.py
Normal file
521
tests/test_code_runner_e2e.py
Normal file
@ -0,0 +1,521 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from wrenn.code_runner import (
|
||||
AsyncCapsule,
|
||||
Capsule,
|
||||
Execution,
|
||||
Result,
|
||||
)
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
|
||||
_env_loaded = False
|
||||
|
||||
|
||||
def _ensure_env() -> None:
|
||||
global _env_loaded
|
||||
if _env_loaded:
|
||||
return
|
||||
_env_loaded = True
|
||||
env_file = Path(__file__).resolve().parent.parent / ".env"
|
||||
if not env_file.exists():
|
||||
return
|
||||
for line in env_file.read_text().splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, _, value = line.partition("=")
|
||||
key, value = key.strip(), value.strip().strip("\"'")
|
||||
if key and key not in os.environ:
|
||||
os.environ[key] = value
|
||||
|
||||
|
||||
# ───────────────────────── Sync e2e ─────────────────────────
|
||||
|
||||
|
||||
class TestCodeRunnerSync:
|
||||
"""Shared capsule — kernel state persists across tests."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_uses_code_runner_beta_template(self):
|
||||
assert self.capsule.info is not None
|
||||
assert self.capsule.info.template == "code-runner-beta"
|
||||
|
||||
def test_default_kernel_name_is_wrenn(self):
|
||||
assert self.capsule._kernel_name == "wrenn"
|
||||
|
||||
def test_simple_expression(self):
|
||||
ex = self.capsule.run_code("1 + 1")
|
||||
assert isinstance(ex, Execution)
|
||||
assert ex.error is None
|
||||
assert ex.text == "2"
|
||||
assert ex.execution_count is not None
|
||||
assert ex.execution_count >= 1
|
||||
|
||||
def test_print_captures_stdout(self):
|
||||
ex = self.capsule.run_code("print('hello world')")
|
||||
assert ex.error is None
|
||||
joined = "".join(ex.logs.stdout)
|
||||
assert "hello world" in joined
|
||||
|
||||
def test_stderr_captured(self):
|
||||
ex = self.capsule.run_code("import sys; sys.stderr.write('an error\\n')")
|
||||
assert ex.error is None
|
||||
joined = "".join(ex.logs.stderr)
|
||||
assert "an error" in joined
|
||||
|
||||
def test_kernel_state_persists_across_calls(self):
|
||||
self.capsule.run_code("persistent_value = 12345")
|
||||
ex = self.capsule.run_code("persistent_value")
|
||||
assert ex.text == "12345"
|
||||
|
||||
def test_import_persists(self):
|
||||
self.capsule.run_code("import math")
|
||||
ex = self.capsule.run_code("round(math.pi, 4)")
|
||||
assert ex.text == "3.1416"
|
||||
|
||||
def test_function_definition_persists(self):
|
||||
self.capsule.run_code(
|
||||
"def fib(n):\n"
|
||||
" a, b = 0, 1\n"
|
||||
" for _ in range(n):\n"
|
||||
" a, b = b, a + b\n"
|
||||
" return a\n"
|
||||
)
|
||||
ex = self.capsule.run_code("fib(10)")
|
||||
assert ex.text == "55"
|
||||
|
||||
def test_class_definition_persists(self):
|
||||
self.capsule.run_code(
|
||||
"class Counter:\n"
|
||||
" def __init__(self): self.n = 0\n"
|
||||
" def inc(self): self.n += 1; return self.n\n"
|
||||
"c = Counter()\n"
|
||||
)
|
||||
ex = self.capsule.run_code("c.inc(); c.inc(); c.inc(); c.n")
|
||||
assert ex.text == "3"
|
||||
|
||||
def test_exception_captured(self):
|
||||
ex = self.capsule.run_code("raise ValueError('boom')")
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "ValueError"
|
||||
assert "boom" in ex.error.value
|
||||
assert "ValueError" in ex.error.traceback
|
||||
|
||||
def test_name_error(self):
|
||||
ex = self.capsule.run_code("undefined_symbol_xyz")
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "NameError"
|
||||
|
||||
def test_syntax_error(self):
|
||||
ex = self.capsule.run_code("def )(\n")
|
||||
assert ex.error is not None
|
||||
assert "SyntaxError" in ex.error.name
|
||||
|
||||
def test_callbacks_fire(self):
|
||||
stdout_chunks: list[str] = []
|
||||
stderr_chunks: list[str] = []
|
||||
results: list[Result] = []
|
||||
errors = []
|
||||
self.capsule.run_code(
|
||||
"import sys\nprint('on stdout')\nsys.stderr.write('on stderr\\n')\n42\n",
|
||||
on_stdout=stdout_chunks.append,
|
||||
on_stderr=stderr_chunks.append,
|
||||
on_result=results.append,
|
||||
on_error=errors.append,
|
||||
)
|
||||
assert any("on stdout" in c for c in stdout_chunks)
|
||||
assert any("on stderr" in c for c in stderr_chunks)
|
||||
assert any(r.text == "42" for r in results)
|
||||
assert errors == []
|
||||
|
||||
def test_multi_line_output(self):
|
||||
ex = self.capsule.run_code("for i in range(3):\n print(i)\n")
|
||||
joined = "".join(ex.logs.stdout)
|
||||
assert "0" in joined and "1" in joined and "2" in joined
|
||||
|
||||
def test_no_main_result_when_statement_only(self):
|
||||
ex = self.capsule.run_code("x = 5")
|
||||
assert ex.text is None
|
||||
assert ex.error is None
|
||||
|
||||
def test_html_repr_result(self):
|
||||
ex = self.capsule.run_code(
|
||||
"from IPython.display import HTML\nHTML('<b>bold</b>')"
|
||||
)
|
||||
assert ex.error is None
|
||||
main = [r for r in ex.results if r.is_main_result]
|
||||
assert main, "expected execute_result"
|
||||
assert main[0].html is not None
|
||||
assert "<b>bold</b>" in main[0].html
|
||||
|
||||
def test_display_data_separate_from_execute_result(self):
|
||||
ex = self.capsule.run_code(
|
||||
"from IPython.display import display, HTML\n"
|
||||
"display(HTML('<i>shown</i>'))\n"
|
||||
"'final'\n"
|
||||
)
|
||||
assert ex.error is None
|
||||
mains = [r for r in ex.results if r.is_main_result]
|
||||
displays = [r for r in ex.results if not r.is_main_result]
|
||||
assert len(mains) == 1
|
||||
assert mains[0].text == "'final'"
|
||||
assert len(displays) >= 1
|
||||
assert any(r.html and "shown" in r.html for r in displays)
|
||||
|
||||
def test_matplotlib_png(self):
|
||||
ex = self.capsule.run_code(
|
||||
"%matplotlib inline\n"
|
||||
"import matplotlib.pyplot as plt\n"
|
||||
"plt.figure()\n"
|
||||
"plt.plot([1,2,3],[4,1,5])\n"
|
||||
"plt.show()\n"
|
||||
)
|
||||
if ex.error is not None and ex.error.name == "ModuleNotFoundError":
|
||||
pytest.skip("matplotlib not in template")
|
||||
assert ex.error is None
|
||||
pngs = [r for r in ex.results if r.png is not None]
|
||||
assert pngs, "expected at least one PNG result from plt.show()"
|
||||
|
||||
def test_pandas_repr(self):
|
||||
ex = self.capsule.run_code(
|
||||
"import pandas as pd\npd.DataFrame({'a':[1,2],'b':[3,4]})\n"
|
||||
)
|
||||
if ex.error is not None and ex.error.name == "ModuleNotFoundError":
|
||||
pytest.skip("pandas not in template")
|
||||
assert ex.error is None
|
||||
main = [r for r in ex.results if r.is_main_result]
|
||||
assert main
|
||||
assert main[0].html is not None or main[0].text is not None
|
||||
|
||||
def test_filesystem_round_trip(self):
|
||||
self.capsule.run_code(
|
||||
"with open('/tmp/from_kernel.txt','w') as f: f.write('written-by-kernel')"
|
||||
)
|
||||
content = self.capsule.files.read("/tmp/from_kernel.txt")
|
||||
assert content == "written-by-kernel"
|
||||
|
||||
def test_text_preserves_string_repr(self):
|
||||
"""Strings keep their surrounding quotes — the ``text/plain`` MIME
|
||||
is the Jupyter repr, which is what disambiguates ``'2'`` from
|
||||
``2``."""
|
||||
ex = self.capsule.run_code("'hello'")
|
||||
assert ex.text == "'hello'"
|
||||
ex = self.capsule.run_code('"with\\"inside"')
|
||||
assert ex.text is not None
|
||||
assert ex.text.startswith("'") or ex.text.startswith('"')
|
||||
ex = self.capsule.run_code("42")
|
||||
assert ex.text == "42"
|
||||
ex = self.capsule.run_code("[1, 2, 3]")
|
||||
assert ex.text == "[1, 2, 3]"
|
||||
ex = self.capsule.run_code("{'k': 'v'}")
|
||||
assert ex.text == "{'k': 'v'}"
|
||||
|
||||
def test_kernel_id_cached(self):
|
||||
first = self.capsule._kernel_id
|
||||
self.capsule.run_code("1")
|
||||
assert self.capsule._kernel_id == first
|
||||
|
||||
def test_complex_workflow(self):
|
||||
ex = self.capsule.run_code(
|
||||
"import json\n"
|
||||
"data = [{'n': i, 'sq': i*i} for i in range(5)]\n"
|
||||
"print(json.dumps(data))\n"
|
||||
"sum(d['sq'] for d in data)\n"
|
||||
)
|
||||
assert ex.error is None
|
||||
assert ex.text == "30"
|
||||
assert any('"sq": 16' in c for c in ex.logs.stdout)
|
||||
|
||||
|
||||
class TestCodeRunnerMimeTypes:
|
||||
"""Cover every non-text MIME field on ``Result`` using the libs
|
||||
baked into the ``code-runner-beta`` template
|
||||
(numpy, pandas, matplotlib, seaborn, requests)."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _run(self, code: str) -> Execution:
|
||||
ex = self.capsule.run_code(code, timeout=60)
|
||||
assert ex.error is None, f"unexpected error: {ex.error}"
|
||||
return ex
|
||||
|
||||
# ── html ──────────────────────────────────────────────────────
|
||||
def test_html_via_ipython_display(self):
|
||||
ex = self._run(
|
||||
"from IPython.display import HTML\nHTML('<table><tr><td>x</td></tr></table>')"
|
||||
)
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
assert main.html is not None
|
||||
assert "<table>" in main.html
|
||||
assert "html" in main.formats()
|
||||
|
||||
def test_html_via_pandas_dataframe(self):
|
||||
ex = self._run(
|
||||
"import pandas as pd\n"
|
||||
"pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'y', 'z']})\n"
|
||||
)
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
assert main.html is not None
|
||||
# pandas emits a styled <table>
|
||||
assert "<table" in main.html
|
||||
assert "dataframe" in main.html.lower() or "<tr" in main.html
|
||||
# text/plain still present alongside html
|
||||
assert main.text is not None
|
||||
|
||||
# ── markdown ──────────────────────────────────────────────────
|
||||
def test_markdown(self):
|
||||
ex = self._run(
|
||||
"from IPython.display import Markdown\nMarkdown('# heading\\n* a\\n* b')"
|
||||
)
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
assert main.markdown is not None
|
||||
assert "# heading" in main.markdown
|
||||
assert "markdown" in main.formats()
|
||||
|
||||
# ── json ──────────────────────────────────────────────────────
|
||||
def test_json_bundle(self):
|
||||
ex = self._run(
|
||||
"from IPython.display import JSON\nJSON({'a': 1, 'nested': {'b': [1, 2]}})"
|
||||
)
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
# IPython.display.JSON emits application/json
|
||||
assert main.json is not None
|
||||
assert main.json == {"a": 1, "nested": {"b": [1, 2]}}
|
||||
assert "json" in main.formats()
|
||||
|
||||
# ── latex ─────────────────────────────────────────────────────
|
||||
def test_latex(self):
|
||||
ex = self._run("from IPython.display import Latex\nLatex(r'$E = mc^2$')")
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
assert main.latex is not None
|
||||
assert "mc^2" in main.latex
|
||||
|
||||
# ── svg ───────────────────────────────────────────────────────
|
||||
def test_svg(self):
|
||||
svg_payload = (
|
||||
'<svg xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"10\\" height=\\"10\\">'
|
||||
'<rect width=\\"10\\" height=\\"10\\" fill=\\"red\\"/></svg>'
|
||||
)
|
||||
ex = self._run(f"from IPython.display import SVG\nSVG(data='{svg_payload}')")
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
assert main.svg is not None
|
||||
assert "<svg" in main.svg
|
||||
assert "<rect" in main.svg
|
||||
|
||||
# ── javascript ────────────────────────────────────────────────
|
||||
def test_javascript(self):
|
||||
ex = self._run(
|
||||
"from IPython.display import Javascript\nJavascript('console.log(\"hi\")')"
|
||||
)
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
# Some IPython versions only emit text/plain for Javascript;
|
||||
# accept either javascript or extra/application/javascript.
|
||||
js = main.javascript or (main.extra or {}).get("application/javascript")
|
||||
assert js is not None, f"no js payload, got formats: {main.formats()}"
|
||||
assert "console.log" in js
|
||||
|
||||
# ── png (matplotlib) ──────────────────────────────────────────
|
||||
def test_png_from_matplotlib(self):
|
||||
ex = self._run(
|
||||
"%matplotlib inline\n"
|
||||
"import matplotlib.pyplot as plt\n"
|
||||
"import numpy as np\n"
|
||||
"x = np.linspace(0, 6.28, 100)\n"
|
||||
"plt.figure()\n"
|
||||
"plt.plot(x, np.sin(x))\n"
|
||||
"plt.title('sine')\n"
|
||||
"plt.show()\n"
|
||||
)
|
||||
pngs = [r for r in ex.results if r.png is not None]
|
||||
assert pngs, "expected PNG from plt.show()"
|
||||
# Base64 PNG starts with iVBORw0KGgo (== PNG magic in base64)
|
||||
assert pngs[0].png.startswith("iVBORw0KGgo")
|
||||
assert "png" in pngs[0].formats()
|
||||
|
||||
def test_png_from_seaborn(self):
|
||||
ex = self._run(
|
||||
"%matplotlib inline\n"
|
||||
"import matplotlib.pyplot as plt\n"
|
||||
"import seaborn as sns\n"
|
||||
"import pandas as pd\n"
|
||||
"df = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [10, 20, 15, 25]})\n"
|
||||
"plt.figure()\n"
|
||||
"sns.barplot(data=df, x='x', y='y')\n"
|
||||
"plt.show()\n"
|
||||
)
|
||||
pngs = [r for r in ex.results if r.png is not None]
|
||||
assert pngs, "expected PNG from seaborn plot"
|
||||
assert pngs[0].png.startswith("iVBORw0KGgo")
|
||||
|
||||
# ── jpeg ──────────────────────────────────────────────────────
|
||||
def test_jpeg_via_matplotlib(self):
|
||||
ex = self._run(
|
||||
"%matplotlib inline\n"
|
||||
"import matplotlib.pyplot as plt\n"
|
||||
"import matplotlib_inline.backend_inline as bi\n"
|
||||
"bi.set_matplotlib_formats('jpeg')\n"
|
||||
"plt.figure()\n"
|
||||
"plt.plot([1, 2, 3])\n"
|
||||
"plt.show()\n"
|
||||
"bi.set_matplotlib_formats('png')\n"
|
||||
)
|
||||
jpegs = [r for r in ex.results if r.jpeg is not None]
|
||||
if not jpegs:
|
||||
pytest.skip("matplotlib_inline jpeg backend unavailable")
|
||||
# JPEG magic in base64 starts with /9j/
|
||||
assert jpegs[0].jpeg.startswith("/9j/")
|
||||
|
||||
# ── multi-format bundle ───────────────────────────────────────
|
||||
def test_pandas_emits_text_and_html(self):
|
||||
ex = self._run("import pandas as pd\npd.DataFrame({'n': range(3)})")
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
fmts = main.formats()
|
||||
assert "text" in fmts
|
||||
assert "html" in fmts
|
||||
assert main.is_main_result is True
|
||||
|
||||
def test_matplotlib_figure_emits_png_and_text(self):
|
||||
ex = self._run(
|
||||
"%matplotlib inline\n"
|
||||
"import matplotlib.pyplot as plt\n"
|
||||
"fig, ax = plt.subplots()\n"
|
||||
"ax.plot([1, 2, 3])\n"
|
||||
"fig\n" # return the figure as the last expression
|
||||
)
|
||||
main = next(r for r in ex.results if r.is_main_result)
|
||||
fmts = main.formats()
|
||||
# Figure repr bundles both text and png.
|
||||
assert "png" in fmts
|
||||
assert "text" in fmts
|
||||
|
||||
# ── numpy / requests round-trips through .text ────────────────
|
||||
def test_numpy_array_text_repr(self):
|
||||
ex = self._run("import numpy as np\nnp.arange(5)")
|
||||
assert ex.text is not None
|
||||
assert "array([0, 1, 2, 3, 4])" in ex.text
|
||||
|
||||
def test_requests_status_code(self):
|
||||
ex = self._run(
|
||||
"import requests\n"
|
||||
"r = requests.get('https://httpbin.org/status/204', timeout=10)\n"
|
||||
"r.status_code\n"
|
||||
)
|
||||
if ex.error is not None:
|
||||
pytest.skip(f"network unavailable: {ex.error.name}")
|
||||
assert ex.text == "204"
|
||||
|
||||
|
||||
class TestCodeRunnerIsolation:
|
||||
"""Each test gets its own capsule — verifies fresh-kernel boot."""
|
||||
|
||||
def setup_method(self):
|
||||
_ensure_env()
|
||||
|
||||
def test_fresh_capsule_no_state_leak(self):
|
||||
c1 = Capsule(wait=True)
|
||||
try:
|
||||
c1.run_code("leaked = 'c1'")
|
||||
c2 = Capsule(wait=True)
|
||||
try:
|
||||
ex = c2.run_code("leaked")
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "NameError"
|
||||
finally:
|
||||
c2.destroy()
|
||||
finally:
|
||||
c1.destroy()
|
||||
|
||||
def test_context_manager(self):
|
||||
with Capsule(wait=True) as c:
|
||||
ex = c.run_code("'ctx'")
|
||||
assert ex.text == "'ctx'"
|
||||
|
||||
def test_deprecated_code_interpreter_import_still_works(self):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", FutureWarning)
|
||||
from wrenn.code_interpreter import Capsule as LegacyCapsule
|
||||
with LegacyCapsule(wait=True) as c:
|
||||
ex = c.run_code("'legacy'")
|
||||
assert ex.text == "'legacy'"
|
||||
|
||||
|
||||
# ───────────────────────── Async e2e ─────────────────────────
|
||||
|
||||
|
||||
class TestCodeRunnerAsync:
|
||||
def setup_method(self):
|
||||
_ensure_env()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_simple(self):
|
||||
async with await AsyncCapsule.create(wait=True) as c:
|
||||
ex = await c.run_code("21 * 2")
|
||||
assert ex.error is None
|
||||
assert ex.text == "42"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_persistence(self):
|
||||
async with await AsyncCapsule.create(wait=True) as c:
|
||||
await c.run_code("v = 'persisted'")
|
||||
ex = await c.run_code("v")
|
||||
assert ex.text == "'persisted'"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_callbacks(self):
|
||||
async with await AsyncCapsule.create(wait=True) as c:
|
||||
chunks: list[str] = []
|
||||
await c.run_code(
|
||||
"print('async out')",
|
||||
on_stdout=chunks.append,
|
||||
)
|
||||
assert any("async out" in s for s in chunks)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_context_manager(self):
|
||||
async with await AsyncCapsule.create(wait=True) as c:
|
||||
ex = await c.run_code("'in-ctx'")
|
||||
assert ex.text == "'in-ctx'"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_concurrent_capsules(self):
|
||||
async with await AsyncCapsule.create(wait=True) as c1:
|
||||
async with await AsyncCapsule.create(wait=True) as c2:
|
||||
r1, r2 = await asyncio.gather(
|
||||
c1.run_code("1 + 1"),
|
||||
c2.run_code("10 * 10"),
|
||||
)
|
||||
assert r1.text == "2"
|
||||
assert r2.text == "100"
|
||||
887
tests/test_code_runner_unit.py
Normal file
887
tests/test_code_runner_unit.py
Normal file
@ -0,0 +1,887 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import json
|
||||
import sys
|
||||
import warnings
|
||||
from unittest.mock import patch
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from wrenn.code_runner import (
|
||||
AsyncCapsule,
|
||||
Capsule,
|
||||
Execution,
|
||||
Logs,
|
||||
Result,
|
||||
)
|
||||
from wrenn.code_runner.capsule import DEFAULT_KERNEL, DEFAULT_TEMPLATE
|
||||
|
||||
BASE = "https://app.wrenn.dev/api"
|
||||
API_KEY = "wrn_test1234567890abcdef12345678"
|
||||
|
||||
|
||||
# ───────────────────────── Result / Execution models ─────────────────────────
|
||||
|
||||
|
||||
class TestResultFromBundle:
|
||||
def test_unpacks_known_mime_types(self):
|
||||
r = Result.from_bundle(
|
||||
{
|
||||
"text/plain": "42",
|
||||
"text/html": "<b>42</b>",
|
||||
"image/png": "iVBORw0KGgo=",
|
||||
"application/json": {"x": 1},
|
||||
},
|
||||
is_main_result=True,
|
||||
)
|
||||
assert r.text == "42"
|
||||
assert r.html == "<b>42</b>"
|
||||
assert r.png == "iVBORw0KGgo="
|
||||
assert r.json == {"x": 1}
|
||||
assert r.is_main_result is True
|
||||
assert r.extra is None
|
||||
|
||||
def test_unknown_mime_lands_in_extra(self):
|
||||
r = Result.from_bundle({"application/vnd.custom+json": "{}"})
|
||||
assert r.extra == {"application/vnd.custom+json": "{}"}
|
||||
assert r.is_main_result is False
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw",
|
||||
[
|
||||
"'hello'",
|
||||
'"hello"',
|
||||
"hello",
|
||||
"'x",
|
||||
"''",
|
||||
"'",
|
||||
"'it\\'s'",
|
||||
"{'a': 1}",
|
||||
"[1, 2, 3]",
|
||||
],
|
||||
)
|
||||
def test_text_plain_preserved_verbatim(self, raw):
|
||||
"""``text/plain`` is the Jupyter repr — pass through unchanged.
|
||||
Stripping outer quotes would lose string identity (a string
|
||||
``'2'`` would become indistinguishable from the int ``2``)."""
|
||||
r = Result.from_bundle({"text/plain": raw})
|
||||
assert r.text == raw
|
||||
|
||||
def test_formats_lists_present_fields(self):
|
||||
r = Result.from_bundle({"text/plain": "x", "image/svg+xml": "<svg/>"})
|
||||
fmts = r.formats()
|
||||
assert "text" in fmts
|
||||
assert "svg" in fmts
|
||||
assert "html" not in fmts
|
||||
|
||||
def test_formats_includes_extra(self):
|
||||
r = Result.from_bundle({"application/x-foo": "bar"})
|
||||
assert "application/x-foo" in r.formats()
|
||||
|
||||
def test_all_mime_types_map(self):
|
||||
r = Result.from_bundle(
|
||||
{
|
||||
"text/plain": "a",
|
||||
"text/html": "b",
|
||||
"text/markdown": "c",
|
||||
"image/svg+xml": "d",
|
||||
"image/png": "e",
|
||||
"image/jpeg": "f",
|
||||
"application/pdf": "g",
|
||||
"text/latex": "h",
|
||||
"application/json": {"k": 1},
|
||||
"application/javascript": "j",
|
||||
}
|
||||
)
|
||||
for attr in (
|
||||
"text",
|
||||
"html",
|
||||
"markdown",
|
||||
"svg",
|
||||
"png",
|
||||
"jpeg",
|
||||
"pdf",
|
||||
"latex",
|
||||
"json",
|
||||
"javascript",
|
||||
):
|
||||
assert getattr(r, attr) is not None
|
||||
|
||||
|
||||
class TestExecution:
|
||||
def test_text_returns_main_result(self):
|
||||
ex = Execution(
|
||||
results=[
|
||||
Result(text="display", is_main_result=False),
|
||||
Result(text="main", is_main_result=True),
|
||||
]
|
||||
)
|
||||
assert ex.text == "main"
|
||||
|
||||
def test_text_none_when_no_main(self):
|
||||
ex = Execution(results=[Result(text="x", is_main_result=False)])
|
||||
assert ex.text is None
|
||||
|
||||
def test_defaults(self):
|
||||
ex = Execution()
|
||||
assert ex.results == []
|
||||
assert isinstance(ex.logs, Logs)
|
||||
assert ex.error is None
|
||||
assert ex.execution_count is None
|
||||
|
||||
|
||||
# ───────────────────────── deprecation alias ─────────────────────────
|
||||
|
||||
|
||||
class TestDeprecationAlias:
|
||||
def test_code_interpreter_emits_warning_on_import(self):
|
||||
# Force a fresh import to observe the warning.
|
||||
sys.modules.pop("wrenn.code_interpreter", None)
|
||||
# Reset the one-shot flag in case the module was previously imported.
|
||||
with warnings.catch_warnings(record=True) as captured:
|
||||
warnings.simplefilter("always")
|
||||
ci = importlib.import_module("wrenn.code_interpreter")
|
||||
ci.warnings_emitted = False # type: ignore[attr-defined]
|
||||
# Re-import to trigger again
|
||||
sys.modules.pop("wrenn.code_interpreter", None)
|
||||
importlib.import_module("wrenn.code_interpreter")
|
||||
msgs = [
|
||||
str(w.message)
|
||||
for w in captured
|
||||
if issubclass(w.category, FutureWarning)
|
||||
]
|
||||
assert any("code_interpreter" in m and "code_runner" in m for m in msgs)
|
||||
|
||||
def test_alias_re_exports_same_classes(self):
|
||||
from wrenn import code_interpreter as ci
|
||||
|
||||
assert ci.Capsule is Capsule
|
||||
assert ci.AsyncCapsule is AsyncCapsule
|
||||
assert ci.Execution is Execution
|
||||
assert ci.Result is Result
|
||||
|
||||
def test_sandbox_attr_deprecated(self):
|
||||
from wrenn import code_runner as cr
|
||||
|
||||
with warnings.catch_warnings(record=True) as captured:
|
||||
warnings.simplefilter("always")
|
||||
S = cr.Sandbox
|
||||
assert S is cr.Capsule
|
||||
assert any(
|
||||
issubclass(w.category, FutureWarning) and "Sandbox" in str(w.message)
|
||||
for w in captured
|
||||
)
|
||||
|
||||
|
||||
# ───────────────────────── Capsule (mock HTTP) ─────────────────────────
|
||||
|
||||
|
||||
@respx.mock
|
||||
def _make_capsule(capsule_id: str = "sb-1") -> Capsule:
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202,
|
||||
json={"id": capsule_id, "status": "starting", "template": DEFAULT_TEMPLATE},
|
||||
)
|
||||
return Capsule(api_key=API_KEY, base_url=BASE)
|
||||
|
||||
|
||||
class TestCapsuleDefaults:
|
||||
@respx.mock
|
||||
def test_default_template_sent(self):
|
||||
route = respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-1", "status": "starting"}
|
||||
)
|
||||
Capsule(api_key=API_KEY, base_url=BASE)
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["template"] == DEFAULT_TEMPLATE
|
||||
assert DEFAULT_TEMPLATE == "code-runner-beta"
|
||||
|
||||
@respx.mock
|
||||
def test_explicit_template_override(self):
|
||||
route = respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-1", "status": "starting"}
|
||||
)
|
||||
Capsule(template="other-template", api_key=API_KEY, base_url=BASE)
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["template"] == "other-template"
|
||||
|
||||
@respx.mock
|
||||
def test_create_classmethod(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-2", "status": "starting"}
|
||||
)
|
||||
c = Capsule.create(api_key=API_KEY, base_url=BASE)
|
||||
assert c.capsule_id == "sb-2"
|
||||
|
||||
@respx.mock
|
||||
def test_default_kernel_name(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-1", "status": "starting"}
|
||||
)
|
||||
c = Capsule(api_key=API_KEY, base_url=BASE)
|
||||
assert c._kernel_name == DEFAULT_KERNEL == "wrenn"
|
||||
|
||||
@respx.mock
|
||||
def test_custom_kernel_name(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-1", "status": "starting"}
|
||||
)
|
||||
c = Capsule(kernel="python3", api_key=API_KEY, base_url=BASE)
|
||||
assert c._kernel_name == "python3"
|
||||
|
||||
|
||||
class TestCtorFailureSafe:
|
||||
"""Bug regression: __del__ must not crash when ctor fails before
|
||||
_proxy_client is initialised."""
|
||||
|
||||
@respx.mock
|
||||
def test_del_safe_when_ctor_fails(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
404,
|
||||
json={"error": {"code": "not_found", "message": "no template"}},
|
||||
)
|
||||
from wrenn.exceptions import WrennNotFoundError
|
||||
|
||||
with pytest.raises(WrennNotFoundError):
|
||||
Capsule(api_key=API_KEY, base_url=BASE)
|
||||
# If we got here without an AttributeError on __del__, we're good.
|
||||
|
||||
@respx.mock
|
||||
def test_close_idempotent(self):
|
||||
c = _make_capsule()
|
||||
c.close()
|
||||
c.close() # second call must not raise
|
||||
|
||||
|
||||
# ───────────────────────── _ensure_kernel ─────────────────────────
|
||||
|
||||
|
||||
class TestEnsureKernel:
|
||||
@respx.mock
|
||||
def test_creates_kernel_with_wrenn_name_when_none_exist(self):
|
||||
c = _make_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
list_route = respx.get(f"{proxy_base}/api/kernels").respond(200, json=[])
|
||||
create_route = respx.post(f"{proxy_base}/api/kernels").respond(
|
||||
201, json={"id": "k-new", "name": "wrenn"}
|
||||
)
|
||||
|
||||
kid = c._ensure_kernel()
|
||||
assert kid == "k-new"
|
||||
# Body must request the wrenn kernelspec.
|
||||
body = json.loads(create_route.calls[0].request.content)
|
||||
assert body == {"name": "wrenn"}
|
||||
assert list_route.called
|
||||
|
||||
@respx.mock
|
||||
def test_reuses_existing_wrenn_kernel(self):
|
||||
c = _make_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
respx.get(f"{proxy_base}/api/kernels").respond(
|
||||
200,
|
||||
json=[
|
||||
{"id": "k-other", "name": "python3"},
|
||||
{"id": "k-wrenn", "name": "wrenn"},
|
||||
],
|
||||
)
|
||||
create = respx.post(f"{proxy_base}/api/kernels").respond(201, json={})
|
||||
kid = c._ensure_kernel()
|
||||
assert kid == "k-wrenn"
|
||||
assert not create.called
|
||||
|
||||
@respx.mock
|
||||
def test_creates_when_only_other_kernels_exist(self):
|
||||
c = _make_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
respx.get(f"{proxy_base}/api/kernels").respond(
|
||||
200, json=[{"id": "k-other", "name": "python3"}]
|
||||
)
|
||||
respx.post(f"{proxy_base}/api/kernels").respond(
|
||||
201, json={"id": "k-new", "name": "wrenn"}
|
||||
)
|
||||
kid = c._ensure_kernel()
|
||||
assert kid == "k-new"
|
||||
|
||||
@respx.mock
|
||||
def test_caches_kernel_id(self):
|
||||
c = _make_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
route = respx.get(f"{proxy_base}/api/kernels").respond(
|
||||
200, json=[{"id": "k-1", "name": "wrenn"}]
|
||||
)
|
||||
c._ensure_kernel()
|
||||
c._ensure_kernel()
|
||||
assert route.call_count == 1
|
||||
|
||||
@respx.mock
|
||||
def test_custom_kernel_name_sent(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-1", "status": "starting"}
|
||||
)
|
||||
c = Capsule(kernel="python3", api_key=API_KEY, base_url=BASE)
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
respx.get(f"{proxy_base}/api/kernels").respond(200, json=[])
|
||||
create = respx.post(f"{proxy_base}/api/kernels").respond(
|
||||
201, json={"id": "k-py", "name": "python3"}
|
||||
)
|
||||
c._ensure_kernel()
|
||||
body = json.loads(create.calls[0].request.content)
|
||||
assert body == {"name": "python3"}
|
||||
|
||||
@respx.mock
|
||||
def test_retries_on_5xx_then_succeeds(self):
|
||||
c = _make_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
responses = [
|
||||
httpx.Response(503),
|
||||
httpx.Response(200, json=[{"id": "k-1", "name": "wrenn"}]),
|
||||
]
|
||||
respx.get(f"{proxy_base}/api/kernels").mock(side_effect=responses)
|
||||
with patch("time.sleep"):
|
||||
kid = c._ensure_kernel(jupyter_timeout=5)
|
||||
assert kid == "k-1"
|
||||
|
||||
@respx.mock
|
||||
def test_raises_on_4xx(self):
|
||||
c = _make_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
respx.get(f"{proxy_base}/api/kernels").respond(401)
|
||||
with pytest.raises(httpx.HTTPStatusError):
|
||||
c._ensure_kernel(jupyter_timeout=2)
|
||||
|
||||
@respx.mock
|
||||
def test_timeout_raises(self):
|
||||
c = _make_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
respx.get(f"{proxy_base}/api/kernels").respond(503)
|
||||
with patch("time.sleep"):
|
||||
with pytest.raises(TimeoutError):
|
||||
c._ensure_kernel(jupyter_timeout=0.01)
|
||||
|
||||
|
||||
# ───────────────────────── build_execute_request ─────────────────────────
|
||||
|
||||
|
||||
class TestJupyterRequest:
|
||||
def test_structure(self):
|
||||
from wrenn.code_runner._protocol import build_execute_request
|
||||
|
||||
msg = build_execute_request("print(1)")
|
||||
assert msg["channel"] == "shell"
|
||||
assert msg["header"]["msg_type"] == "execute_request"
|
||||
assert msg["content"]["code"] == "print(1)"
|
||||
assert msg["content"]["silent"] is False
|
||||
assert msg["content"]["store_history"] is True
|
||||
assert msg["content"]["allow_stdin"] is False
|
||||
assert msg["content"]["stop_on_error"] is True
|
||||
# msg_id must be a uuid-shaped string
|
||||
assert len(msg["header"]["msg_id"]) == 36
|
||||
|
||||
def test_unique_msg_id_per_call(self):
|
||||
from wrenn.code_runner._protocol import build_execute_request
|
||||
|
||||
a = build_execute_request("x")
|
||||
b = build_execute_request("x")
|
||||
assert a["header"]["msg_id"] != b["header"]["msg_id"]
|
||||
|
||||
|
||||
# ───────────────────────── run_code (WS-mocked) ─────────────────────────
|
||||
|
||||
|
||||
def _wrap(msg_type: str, parent_id: str, content: dict) -> dict:
|
||||
return {
|
||||
"msg_type": msg_type,
|
||||
"header": {"msg_type": msg_type},
|
||||
"parent_header": {"msg_id": parent_id},
|
||||
"content": content,
|
||||
}
|
||||
|
||||
|
||||
class _FakeWS:
|
||||
"""Minimal sync httpx_ws-shaped fake.
|
||||
|
||||
If ``frames_factory`` yields an ``Exception`` instance, the fake
|
||||
raises it instead of returning the value — useful for testing
|
||||
disconnect / network-error paths.
|
||||
"""
|
||||
|
||||
def __init__(self, frames_factory):
|
||||
self._frames_factory = frames_factory
|
||||
self._sent: list[str] = []
|
||||
self._iter = None
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *a):
|
||||
return False
|
||||
|
||||
def send_text(self, s: str) -> None:
|
||||
self._sent.append(s)
|
||||
parent_id = json.loads(s)["header"]["msg_id"]
|
||||
self._iter = iter(self._frames_factory(parent_id))
|
||||
|
||||
def receive_json(self, timeout: float = 0):
|
||||
assert self._iter is not None
|
||||
try:
|
||||
nxt = next(self._iter)
|
||||
except StopIteration:
|
||||
raise TimeoutError("no more frames")
|
||||
if isinstance(nxt, BaseException):
|
||||
raise nxt
|
||||
return nxt
|
||||
|
||||
|
||||
class _FakeAsyncWS:
|
||||
def __init__(self, frames_factory):
|
||||
self._frames_factory = frames_factory
|
||||
self._iter = None
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *a):
|
||||
return False
|
||||
|
||||
async def send_text(self, s: str) -> None:
|
||||
parent_id = json.loads(s)["header"]["msg_id"]
|
||||
self._iter = iter(self._frames_factory(parent_id))
|
||||
|
||||
async def receive_json(self):
|
||||
assert self._iter is not None
|
||||
try:
|
||||
nxt = next(self._iter)
|
||||
except StopIteration:
|
||||
raise TimeoutError("no more frames")
|
||||
if isinstance(nxt, BaseException):
|
||||
raise nxt
|
||||
return nxt
|
||||
|
||||
|
||||
class TestRunCode:
|
||||
@respx.mock
|
||||
def _make_ready(self):
|
||||
c = _make_capsule()
|
||||
# Pre-populate kernel so run_code skips ensure.
|
||||
c._kernel_id = "k-1"
|
||||
return c
|
||||
|
||||
def test_stream_stdout_and_stderr(self):
|
||||
c = self._make_ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("stream", pid, {"name": "stdout", "text": "hello\n"})
|
||||
yield _wrap("stream", pid, {"name": "stderr", "text": "warn\n"})
|
||||
yield _wrap("status", pid, {"execution_state": "idle"})
|
||||
|
||||
stdout_chunks, stderr_chunks = [], []
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code(
|
||||
"print('hello')",
|
||||
on_stdout=stdout_chunks.append,
|
||||
on_stderr=stderr_chunks.append,
|
||||
)
|
||||
assert ex.logs.stdout == ["hello\n"]
|
||||
assert ex.logs.stderr == ["warn\n"]
|
||||
assert stdout_chunks == ["hello\n"]
|
||||
assert stderr_chunks == ["warn\n"]
|
||||
assert ex.error is None
|
||||
|
||||
def test_execute_result_main_and_display_data(self):
|
||||
c = self._make_ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap(
|
||||
"display_data",
|
||||
pid,
|
||||
{"data": {"image/png": "BASE64"}},
|
||||
)
|
||||
yield _wrap(
|
||||
"execute_result",
|
||||
pid,
|
||||
{
|
||||
"execution_count": 7,
|
||||
"data": {"text/plain": "'42'"},
|
||||
},
|
||||
)
|
||||
yield _wrap("status", pid, {"execution_state": "idle"})
|
||||
|
||||
results = []
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code("'42'", on_result=results.append)
|
||||
assert ex.execution_count == 7
|
||||
assert len(ex.results) == 2
|
||||
main = [r for r in ex.results if r.is_main_result]
|
||||
assert len(main) == 1
|
||||
assert main[0].text == "'42'" # text/plain preserved verbatim
|
||||
display = [r for r in ex.results if not r.is_main_result]
|
||||
assert display[0].png == "BASE64"
|
||||
assert ex.text == "'42'"
|
||||
assert len(results) == 2
|
||||
|
||||
def test_error_message(self):
|
||||
c = self._make_ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap(
|
||||
"error",
|
||||
pid,
|
||||
{
|
||||
"ename": "NameError",
|
||||
"evalue": "name 'x' is not defined",
|
||||
"traceback": ["line1", "line2"],
|
||||
},
|
||||
)
|
||||
yield _wrap("status", pid, {"execution_state": "idle"})
|
||||
|
||||
errors = []
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code("x", on_error=errors.append)
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "NameError"
|
||||
assert ex.error.value == "name 'x' is not defined"
|
||||
assert ex.error.traceback == "line1\nline2"
|
||||
assert len(errors) == 1
|
||||
|
||||
def test_ignores_frames_with_other_parent(self):
|
||||
c = self._make_ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("stream", "other-id", {"name": "stdout", "text": "drop\n"})
|
||||
yield _wrap("stream", pid, {"name": "stdout", "text": "keep\n"})
|
||||
yield _wrap("status", pid, {"execution_state": "idle"})
|
||||
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code("print('keep')")
|
||||
assert ex.logs.stdout == ["keep\n"]
|
||||
|
||||
def test_unsupported_language_raises(self):
|
||||
c = self._make_ready()
|
||||
with pytest.raises(ValueError, match="not supported"):
|
||||
c.run_code("console.log('x')", language="javascript")
|
||||
|
||||
def test_idle_status_terminates_loop(self):
|
||||
c = self._make_ready()
|
||||
called = {"n": 0}
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("status", pid, {"execution_state": "idle"})
|
||||
# Following frame must never be consumed.
|
||||
called["n"] += 1
|
||||
yield _wrap("stream", pid, {"name": "stdout", "text": "post-idle\n"})
|
||||
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code("pass")
|
||||
assert ex.logs.stdout == []
|
||||
|
||||
|
||||
class TestAsyncRunCode:
|
||||
@respx.mock
|
||||
def _make_ready(self):
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
202, json={"id": "sb-1", "status": "starting"}
|
||||
)
|
||||
from wrenn.client import AsyncWrennClient
|
||||
from wrenn.models import Capsule as CapsuleModel
|
||||
|
||||
client = AsyncWrennClient(api_key=API_KEY, base_url=BASE)
|
||||
info = CapsuleModel(id="sb-1")
|
||||
c = AsyncCapsule(_capsule_id="sb-1", _client=client, _info=info)
|
||||
c._kernel_id = "k-1"
|
||||
return c
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_and_result(self):
|
||||
c = self._make_ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("stream", pid, {"name": "stdout", "text": "hi\n"})
|
||||
yield _wrap(
|
||||
"execute_result",
|
||||
pid,
|
||||
{"execution_count": 1, "data": {"text/plain": "7"}},
|
||||
)
|
||||
yield _wrap("status", pid, {"execution_state": "idle"})
|
||||
|
||||
with patch(
|
||||
"wrenn.code_runner.async_capsule.httpx_ws.aconnect_ws",
|
||||
return_value=_FakeAsyncWS(frames),
|
||||
):
|
||||
ex = await c.run_code("7")
|
||||
assert ex.logs.stdout == ["hi\n"]
|
||||
assert ex.text == "7"
|
||||
assert ex.execution_count == 1
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_default_kernel(self):
|
||||
c = self._make_ready()
|
||||
assert c._kernel_name == "wrenn"
|
||||
await c.close()
|
||||
|
||||
|
||||
class TestAsyncCtorFailureSafe:
|
||||
def test_del_safe_when_not_constructed(self):
|
||||
# Build without ever calling __init__'s parent path that needs network,
|
||||
# by hand-poking attributes the way create() failure would leave them.
|
||||
c = AsyncCapsule.__new__(AsyncCapsule)
|
||||
# __del__ should be safe even with no attrs.
|
||||
c.__del__()
|
||||
|
||||
|
||||
# ───────────────────────── run_code error-path regressions (B2) ─────────────
|
||||
|
||||
|
||||
class TestRunCodeErrorPaths:
|
||||
"""Sync run_code timeout / disconnect / unexpected-exception behavior."""
|
||||
|
||||
def _ready(self):
|
||||
return TestRunCode()._make_ready()
|
||||
|
||||
def test_timeout_when_no_idle_received(self):
|
||||
c = self._ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("stream", pid, {"name": "stdout", "text": "partial\n"})
|
||||
# No idle frame; loop exits via StopIteration → TimeoutError.
|
||||
|
||||
errors = []
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code("x", on_error=errors.append)
|
||||
assert ex.timed_out is True
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "Timeout"
|
||||
assert "exceeded" in ex.error.value
|
||||
assert ex.logs.stdout == ["partial\n"]
|
||||
assert len(errors) == 1
|
||||
|
||||
def test_disconnect_sets_disconnected_error(self):
|
||||
c = self._ready()
|
||||
import httpx_ws
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("stream", pid, {"name": "stdout", "text": "hi\n"})
|
||||
yield httpx_ws.WebSocketDisconnect(code=1000, reason="bye")
|
||||
|
||||
errors = []
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code("x", on_error=errors.append)
|
||||
assert ex.timed_out is True
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "Disconnected"
|
||||
assert ex.logs.stdout == ["hi\n"]
|
||||
assert len(errors) == 1
|
||||
|
||||
def test_unexpected_exception_propagates(self):
|
||||
c = self._ready()
|
||||
|
||||
def frames(pid):
|
||||
yield RuntimeError("WS broken in unexpected way")
|
||||
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
with pytest.raises(RuntimeError, match="WS broken"):
|
||||
c.run_code("x")
|
||||
|
||||
def test_clean_exit_does_not_set_timed_out(self):
|
||||
c = self._ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("status", pid, {"execution_state": "idle"})
|
||||
|
||||
with patch(
|
||||
"wrenn.code_runner.capsule.httpx_ws.connect_ws",
|
||||
return_value=_FakeWS(frames),
|
||||
):
|
||||
ex = c.run_code("pass")
|
||||
assert ex.timed_out is False
|
||||
assert ex.error is None
|
||||
|
||||
|
||||
# ───────────────────────── Async run_code parity ──────────────────────────
|
||||
|
||||
|
||||
class TestAsyncRunCodeErrorPaths:
|
||||
def _ready(self):
|
||||
return TestAsyncRunCode()._make_ready()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_timeout_when_no_idle(self):
|
||||
c = self._ready()
|
||||
|
||||
def frames(pid):
|
||||
yield _wrap("stream", pid, {"name": "stdout", "text": "partial\n"})
|
||||
|
||||
errors = []
|
||||
with patch(
|
||||
"wrenn.code_runner.async_capsule.httpx_ws.aconnect_ws",
|
||||
return_value=_FakeAsyncWS(frames),
|
||||
):
|
||||
ex = await c.run_code("x", on_error=errors.append)
|
||||
assert ex.timed_out is True
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "Timeout"
|
||||
assert ex.logs.stdout == ["partial\n"]
|
||||
assert len(errors) == 1
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_disconnect_sets_disconnected_error(self):
|
||||
c = self._ready()
|
||||
import httpx_ws
|
||||
|
||||
def frames(pid):
|
||||
yield httpx_ws.WebSocketNetworkError("network blip")
|
||||
|
||||
errors = []
|
||||
with patch(
|
||||
"wrenn.code_runner.async_capsule.httpx_ws.aconnect_ws",
|
||||
return_value=_FakeAsyncWS(frames),
|
||||
):
|
||||
ex = await c.run_code("x", on_error=errors.append)
|
||||
assert ex.timed_out is True
|
||||
assert ex.error is not None
|
||||
assert ex.error.name == "Disconnected"
|
||||
assert len(errors) == 1
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_unexpected_exception_propagates(self):
|
||||
c = self._ready()
|
||||
|
||||
def frames(pid):
|
||||
yield RuntimeError("unexpected WS death")
|
||||
|
||||
with patch(
|
||||
"wrenn.code_runner.async_capsule.httpx_ws.aconnect_ws",
|
||||
return_value=_FakeAsyncWS(frames),
|
||||
):
|
||||
with pytest.raises(RuntimeError, match="unexpected WS"):
|
||||
await c.run_code("x")
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_unsupported_language_raises(self):
|
||||
c = self._ready()
|
||||
with pytest.raises(ValueError, match="not supported"):
|
||||
await c.run_code("console.log('x')", language="javascript")
|
||||
await c.close()
|
||||
|
||||
|
||||
# ───────────────────────── Async _ensure_kernel parity ───────────────────────
|
||||
|
||||
|
||||
@respx.mock
|
||||
def _make_async_capsule(capsule_id: str = "sb-1") -> AsyncCapsule:
|
||||
"""Construct an AsyncCapsule without going through ``create()``."""
|
||||
from wrenn.client import AsyncWrennClient
|
||||
from wrenn.models import Capsule as CapsuleModel
|
||||
|
||||
client = AsyncWrennClient(api_key=API_KEY, base_url=BASE)
|
||||
info = CapsuleModel(id=capsule_id)
|
||||
return AsyncCapsule(_capsule_id=capsule_id, _client=client, _info=info)
|
||||
|
||||
|
||||
class TestAsyncEnsureKernel:
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_creates_kernel_when_none_exist(self):
|
||||
c = _make_async_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
list_route = respx.get(f"{proxy_base}/api/kernels").respond(200, json=[])
|
||||
create_route = respx.post(f"{proxy_base}/api/kernels").respond(
|
||||
201, json={"id": "k-new", "name": "wrenn"}
|
||||
)
|
||||
kid = await c._ensure_kernel()
|
||||
assert kid == "k-new"
|
||||
body = json.loads(create_route.calls[0].request.content)
|
||||
assert body == {"name": "wrenn"}
|
||||
assert list_route.called
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_reuses_existing_wrenn_kernel(self):
|
||||
c = _make_async_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
respx.get(f"{proxy_base}/api/kernels").respond(
|
||||
200,
|
||||
json=[
|
||||
{"id": "k-other", "name": "python3"},
|
||||
{"id": "k-wrenn", "name": "wrenn"},
|
||||
],
|
||||
)
|
||||
create = respx.post(f"{proxy_base}/api/kernels").respond(201, json={})
|
||||
kid = await c._ensure_kernel()
|
||||
assert kid == "k-wrenn"
|
||||
assert not create.called
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_retries_on_5xx_then_succeeds(self):
|
||||
c = _make_async_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
responses = [
|
||||
httpx.Response(503),
|
||||
httpx.Response(200, json=[{"id": "k-1", "name": "wrenn"}]),
|
||||
]
|
||||
respx.get(f"{proxy_base}/api/kernels").mock(side_effect=responses)
|
||||
with patch("asyncio.sleep") as sleep_mock:
|
||||
|
||||
async def _noop(_s):
|
||||
return None
|
||||
|
||||
sleep_mock.side_effect = _noop
|
||||
kid = await c._ensure_kernel(jupyter_timeout=5)
|
||||
assert kid == "k-1"
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_raises_on_4xx(self):
|
||||
c = _make_async_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
respx.get(f"{proxy_base}/api/kernels").respond(401)
|
||||
with pytest.raises(httpx.HTTPStatusError):
|
||||
await c._ensure_kernel(jupyter_timeout=2)
|
||||
await c.close()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_caches_kernel_id(self):
|
||||
c = _make_async_capsule()
|
||||
proxy_base = "https://8888-sb-1.wrenn.dev"
|
||||
route = respx.get(f"{proxy_base}/api/kernels").respond(
|
||||
200, json=[{"id": "k-1", "name": "wrenn"}]
|
||||
)
|
||||
await c._ensure_kernel()
|
||||
await c._ensure_kernel()
|
||||
assert route.call_count == 1
|
||||
await c.close()
|
||||
490
tests/test_commands.py
Normal file
490
tests/test_commands.py
Normal file
@ -0,0 +1,490 @@
|
||||
"""Unit tests for wrenn.commands — Commands / AsyncCommands.
|
||||
|
||||
Covers payload construction (cwd, envs, tag, timeout), foreground/background
|
||||
dispatch, base64 response decoding, stream-event parsing, and the
|
||||
WebSocket-backed ``stream`` / ``connect`` iterators (with a fake WS).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
from contextlib import asynccontextmanager, contextmanager
|
||||
|
||||
import httpx_ws
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from wrenn.client import AsyncWrennClient, WrennClient
|
||||
from wrenn.commands import (
|
||||
AsyncCommands,
|
||||
CommandHandle,
|
||||
CommandResult,
|
||||
Commands,
|
||||
ProcessInfo,
|
||||
StreamErrorEvent,
|
||||
StreamEvent,
|
||||
StreamExitEvent,
|
||||
StreamStartEvent,
|
||||
StreamStderrEvent,
|
||||
StreamStdoutEvent,
|
||||
_decode_exec_response,
|
||||
_parse_stream_event,
|
||||
)
|
||||
|
||||
BASE = "https://app.wrenn.dev/api"
|
||||
CAPSULE_ID = "cl-cmd123"
|
||||
EXEC_URL = f"{BASE}/v1/capsules/{CAPSULE_ID}/exec"
|
||||
PROC_URL = f"{BASE}/v1/capsules/{CAPSULE_ID}/processes"
|
||||
|
||||
|
||||
def _make_commands() -> Commands:
|
||||
client = WrennClient(api_key="wrn_test1234567890abcdef12345678", base_url=BASE)
|
||||
return Commands(CAPSULE_ID, client.http)
|
||||
|
||||
|
||||
def _make_async_commands() -> AsyncCommands:
|
||||
client = AsyncWrennClient(api_key="wrn_test1234567890abcdef12345678", base_url=BASE)
|
||||
return AsyncCommands(CAPSULE_ID, client.http)
|
||||
|
||||
|
||||
# ── _decode_exec_response ─────────────────────────────────────────
|
||||
|
||||
|
||||
class TestDecodeExecResponse:
|
||||
def test_plain_text(self):
|
||||
result = _decode_exec_response(
|
||||
{"stdout": "hello\n", "stderr": "", "exit_code": 0, "duration_ms": 12}
|
||||
)
|
||||
assert isinstance(result, CommandResult)
|
||||
assert result.stdout == "hello\n"
|
||||
assert result.exit_code == 0
|
||||
assert result.duration_ms == 12
|
||||
|
||||
def test_base64_stdout(self):
|
||||
encoded = base64.b64encode(b"binary\xff\x00out").decode()
|
||||
result = _decode_exec_response(
|
||||
{"stdout": encoded, "encoding": "base64", "exit_code": 0}
|
||||
)
|
||||
assert "binary" in result.stdout
|
||||
|
||||
def test_base64_stderr(self):
|
||||
out = base64.b64encode(b"ok").decode()
|
||||
err = base64.b64encode(b"warning").decode()
|
||||
result = _decode_exec_response(
|
||||
{"stdout": out, "stderr": err, "encoding": "base64", "exit_code": 1}
|
||||
)
|
||||
assert result.stdout == "ok"
|
||||
assert result.stderr == "warning"
|
||||
assert result.exit_code == 1
|
||||
|
||||
def test_missing_fields_default(self):
|
||||
result = _decode_exec_response({})
|
||||
assert result.stdout == ""
|
||||
assert result.stderr == ""
|
||||
assert result.exit_code == -1
|
||||
assert result.duration_ms is None
|
||||
|
||||
def test_null_stdout_coerced_to_empty(self):
|
||||
result = _decode_exec_response({"stdout": None, "stderr": None})
|
||||
assert result.stdout == ""
|
||||
assert result.stderr == ""
|
||||
|
||||
|
||||
# ── _parse_stream_event ───────────────────────────────────────────
|
||||
|
||||
|
||||
class TestParseStreamEvent:
|
||||
def test_start(self):
|
||||
event = _parse_stream_event({"type": "start", "pid": 99})
|
||||
assert isinstance(event, StreamStartEvent)
|
||||
assert event.type == "start"
|
||||
assert event.pid == 99
|
||||
|
||||
def test_stdout(self):
|
||||
event = _parse_stream_event({"type": "stdout", "data": "out"})
|
||||
assert isinstance(event, StreamStdoutEvent)
|
||||
assert event.data == "out"
|
||||
|
||||
def test_stderr(self):
|
||||
event = _parse_stream_event({"type": "stderr", "data": "err"})
|
||||
assert isinstance(event, StreamStderrEvent)
|
||||
assert event.data == "err"
|
||||
|
||||
def test_exit(self):
|
||||
event = _parse_stream_event({"type": "exit", "exit_code": 7})
|
||||
assert isinstance(event, StreamExitEvent)
|
||||
assert event.exit_code == 7
|
||||
|
||||
def test_error(self):
|
||||
event = _parse_stream_event({"type": "error", "data": "boom"})
|
||||
assert isinstance(event, StreamErrorEvent)
|
||||
assert event.data == "boom"
|
||||
|
||||
def test_unknown_type(self):
|
||||
event = _parse_stream_event({"type": "weird"})
|
||||
assert isinstance(event, StreamEvent)
|
||||
assert event.type == "weird"
|
||||
|
||||
def test_missing_type(self):
|
||||
event = _parse_stream_event({})
|
||||
assert event.type == "unknown"
|
||||
|
||||
def test_exit_missing_code_defaults(self):
|
||||
event = _parse_stream_event({"type": "exit"})
|
||||
assert isinstance(event, StreamExitEvent)
|
||||
assert event.exit_code == -1
|
||||
|
||||
|
||||
# ── Commands.run — payload construction ───────────────────────────
|
||||
|
||||
|
||||
class TestRunPayload:
|
||||
@respx.mock
|
||||
def test_foreground_basic_payload(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"stdout": "hi", "exit_code": 0})
|
||||
result = _make_commands().run("echo hi")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["cmd"] == "/bin/sh"
|
||||
assert body["args"] == ["-c", "echo hi"]
|
||||
assert body["background"] is False
|
||||
assert body["timeout_sec"] == 30
|
||||
assert result.stdout == "hi"
|
||||
|
||||
@respx.mock
|
||||
def test_cwd_in_payload(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("pwd", cwd="/tmp/work")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["cwd"] == "/tmp/work"
|
||||
|
||||
@respx.mock
|
||||
def test_cwd_omitted_when_none(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("pwd")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert "cwd" not in body
|
||||
|
||||
@respx.mock
|
||||
def test_envs_in_payload(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("env", envs={"FOO": "bar", "BAZ": "qux"})
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["envs"] == {"FOO": "bar", "BAZ": "qux"}
|
||||
|
||||
@respx.mock
|
||||
def test_empty_envs_still_sent(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("env", envs={})
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["envs"] == {}
|
||||
|
||||
@respx.mock
|
||||
def test_tag_in_payload(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("echo x", tag="my-tag")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["tag"] == "my-tag"
|
||||
|
||||
@respx.mock
|
||||
def test_custom_timeout_in_payload(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("sleep 1", timeout=120)
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["timeout_sec"] == 120
|
||||
|
||||
@respx.mock
|
||||
def test_timeout_none_omits_field(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("echo x", timeout=None)
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert "timeout_sec" not in body
|
||||
|
||||
@respx.mock
|
||||
def test_all_kwargs_combined(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"exit_code": 0})
|
||||
_make_commands().run("echo x", timeout=60, envs={"A": "1"}, cwd="/srv", tag="t")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["cwd"] == "/srv"
|
||||
assert body["envs"] == {"A": "1"}
|
||||
assert body["tag"] == "t"
|
||||
assert body["timeout_sec"] == 60
|
||||
|
||||
|
||||
class TestRunBackground:
|
||||
@respx.mock
|
||||
def test_background_returns_handle(self):
|
||||
respx.post(EXEC_URL).respond(200, json={"pid": 1234, "tag": "bg"})
|
||||
handle = _make_commands().run("sleep 100", background=True)
|
||||
assert isinstance(handle, CommandHandle)
|
||||
assert handle.pid == 1234
|
||||
assert handle.tag == "bg"
|
||||
assert handle.capsule_id == CAPSULE_ID
|
||||
|
||||
@respx.mock
|
||||
def test_background_omits_timeout_sec(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"pid": 1, "tag": "x"})
|
||||
_make_commands().run("sleep 100", background=True, timeout=30)
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert "timeout_sec" not in body
|
||||
assert body["background"] is True
|
||||
|
||||
@respx.mock
|
||||
def test_background_carries_cwd_and_envs(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"pid": 5, "tag": "t"})
|
||||
_make_commands().run(
|
||||
"server", background=True, cwd="/app", envs={"PORT": "80"}, tag="srv"
|
||||
)
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["cwd"] == "/app"
|
||||
assert body["envs"] == {"PORT": "80"}
|
||||
assert body["tag"] == "srv"
|
||||
|
||||
@respx.mock
|
||||
def test_background_missing_pid_defaults_zero(self):
|
||||
respx.post(EXEC_URL).respond(200, json={"tag": "x"})
|
||||
handle = _make_commands().run("x", background=True)
|
||||
assert handle.pid == 0
|
||||
|
||||
|
||||
class TestListAndKill:
|
||||
@respx.mock
|
||||
def test_list_parses_processes(self):
|
||||
respx.get(PROC_URL).respond(
|
||||
200,
|
||||
json={
|
||||
"processes": [
|
||||
{
|
||||
"pid": 10,
|
||||
"tag": "web",
|
||||
"cmd": "/bin/sh",
|
||||
"args": ["-c", "serve"],
|
||||
},
|
||||
{"pid": 11},
|
||||
]
|
||||
},
|
||||
)
|
||||
procs = _make_commands().list()
|
||||
assert len(procs) == 2
|
||||
assert isinstance(procs[0], ProcessInfo)
|
||||
assert procs[0].pid == 10
|
||||
assert procs[0].tag == "web"
|
||||
assert procs[0].args == ["-c", "serve"]
|
||||
assert procs[1].pid == 11
|
||||
assert procs[1].tag is None
|
||||
|
||||
@respx.mock
|
||||
def test_list_empty(self):
|
||||
respx.get(PROC_URL).respond(200, json={"processes": []})
|
||||
assert _make_commands().list() == []
|
||||
|
||||
@respx.mock
|
||||
def test_list_missing_key(self):
|
||||
respx.get(PROC_URL).respond(200, json={})
|
||||
assert _make_commands().list() == []
|
||||
|
||||
@respx.mock
|
||||
def test_kill_sends_delete(self):
|
||||
route = respx.delete(f"{PROC_URL}/42").respond(204)
|
||||
_make_commands().kill(42)
|
||||
assert route.called
|
||||
|
||||
@respx.mock
|
||||
def test_kill_unknown_pid_raises(self):
|
||||
from wrenn.exceptions import WrennNotFoundError
|
||||
|
||||
respx.delete(f"{PROC_URL}/999").respond(
|
||||
404, json={"error": {"code": "not_found", "message": "no such process"}}
|
||||
)
|
||||
with pytest.raises(WrennNotFoundError):
|
||||
_make_commands().kill(999)
|
||||
|
||||
|
||||
# ── Fake WebSocket plumbing for stream / connect ──────────────────
|
||||
|
||||
|
||||
class _FakeWS:
|
||||
"""Synchronous fake WebSocket session."""
|
||||
|
||||
def __init__(self, messages: list) -> None:
|
||||
self._messages = list(messages)
|
||||
self.sent: list[str] = []
|
||||
|
||||
def send_text(self, text: str) -> None:
|
||||
self.sent.append(text)
|
||||
|
||||
def receive_json(self) -> dict:
|
||||
if not self._messages:
|
||||
raise httpx_ws.WebSocketDisconnect()
|
||||
msg = self._messages.pop(0)
|
||||
if isinstance(msg, Exception):
|
||||
raise msg
|
||||
return msg
|
||||
|
||||
|
||||
class _AsyncFakeWS:
|
||||
"""Asynchronous fake WebSocket session."""
|
||||
|
||||
def __init__(self, messages: list) -> None:
|
||||
self._messages = list(messages)
|
||||
self.sent: list[str] = []
|
||||
|
||||
async def send_text(self, text: str) -> None:
|
||||
self.sent.append(text)
|
||||
|
||||
async def receive_json(self) -> dict:
|
||||
if not self._messages:
|
||||
raise httpx_ws.WebSocketDisconnect()
|
||||
msg = self._messages.pop(0)
|
||||
if isinstance(msg, Exception):
|
||||
raise msg
|
||||
return msg
|
||||
|
||||
|
||||
def _patch_sync_ws(monkeypatch, ws: _FakeWS) -> None:
|
||||
@contextmanager
|
||||
def _fake_connect(url, client):
|
||||
yield ws
|
||||
|
||||
monkeypatch.setattr("wrenn.commands.httpx_ws.connect_ws", _fake_connect)
|
||||
|
||||
|
||||
def _patch_async_ws(monkeypatch, ws: _AsyncFakeWS) -> None:
|
||||
@asynccontextmanager
|
||||
async def _fake_aconnect(url, client):
|
||||
yield ws
|
||||
|
||||
monkeypatch.setattr("wrenn.commands.httpx_ws.aconnect_ws", _fake_aconnect)
|
||||
|
||||
|
||||
# ── Commands.stream ───────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestStream:
|
||||
def test_stream_sends_shell_wrapped_start(self, monkeypatch):
|
||||
ws = _FakeWS([{"type": "exit", "exit_code": 0}])
|
||||
_patch_sync_ws(monkeypatch, ws)
|
||||
list(_make_commands().stream("echo hi"))
|
||||
start = json.loads(ws.sent[0])
|
||||
assert start == {"type": "start", "cmd": "/bin/sh", "args": ["-c", "echo hi"]}
|
||||
|
||||
def test_stream_with_explicit_args(self, monkeypatch):
|
||||
ws = _FakeWS([{"type": "exit", "exit_code": 0}])
|
||||
_patch_sync_ws(monkeypatch, ws)
|
||||
list(_make_commands().stream("/usr/bin/env", args=["python", "-V"]))
|
||||
start = json.loads(ws.sent[0])
|
||||
assert start == {
|
||||
"type": "start",
|
||||
"cmd": "/usr/bin/env",
|
||||
"args": ["python", "-V"],
|
||||
}
|
||||
|
||||
def test_stream_yields_events_until_exit(self, monkeypatch):
|
||||
ws = _FakeWS(
|
||||
[
|
||||
{"type": "start", "pid": 3},
|
||||
{"type": "stdout", "data": "line1"},
|
||||
{"type": "stderr", "data": "warn"},
|
||||
{"type": "exit", "exit_code": 0},
|
||||
{"type": "stdout", "data": "after-exit-ignored"},
|
||||
]
|
||||
)
|
||||
_patch_sync_ws(monkeypatch, ws)
|
||||
events = list(_make_commands().stream("echo line1"))
|
||||
assert [e.type for e in events] == ["start", "stdout", "stderr", "exit"]
|
||||
|
||||
def test_stream_stops_on_error(self, monkeypatch):
|
||||
ws = _FakeWS([{"type": "error", "data": "fatal"}])
|
||||
_patch_sync_ws(monkeypatch, ws)
|
||||
events = list(_make_commands().stream("bad"))
|
||||
assert len(events) == 1
|
||||
assert events[0].type == "error"
|
||||
|
||||
def test_stream_handles_disconnect(self, monkeypatch):
|
||||
ws = _FakeWS([{"type": "stdout", "data": "x"}]) # then disconnect
|
||||
_patch_sync_ws(monkeypatch, ws)
|
||||
events = list(_make_commands().stream("echo x"))
|
||||
assert [e.type for e in events] == ["stdout"]
|
||||
|
||||
|
||||
# ── Commands.connect ──────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestConnect:
|
||||
def test_connect_yields_until_exit(self, monkeypatch):
|
||||
ws = _FakeWS(
|
||||
[
|
||||
{"type": "stdout", "data": "tick"},
|
||||
{"type": "exit", "exit_code": 0},
|
||||
]
|
||||
)
|
||||
_patch_sync_ws(monkeypatch, ws)
|
||||
events = list(_make_commands().connect(55))
|
||||
assert [e.type for e in events] == ["stdout", "exit"]
|
||||
|
||||
def test_connect_handles_disconnect(self, monkeypatch):
|
||||
ws = _FakeWS([]) # immediate disconnect
|
||||
_patch_sync_ws(monkeypatch, ws)
|
||||
assert list(_make_commands().connect(1)) == []
|
||||
|
||||
|
||||
# ── AsyncCommands ─────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestAsyncCommands:
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_run_payload(self):
|
||||
route = respx.post(EXEC_URL).respond(200, json={"stdout": "hi", "exit_code": 0})
|
||||
cmds = _make_async_commands()
|
||||
result = await cmds.run("echo hi", cwd="/tmp", envs={"K": "v"}, tag="z")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["cwd"] == "/tmp"
|
||||
assert body["envs"] == {"K": "v"}
|
||||
assert body["tag"] == "z"
|
||||
assert result.stdout == "hi"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_run_background(self):
|
||||
respx.post(EXEC_URL).respond(200, json={"pid": 7, "tag": "bg"})
|
||||
handle = await _make_async_commands().run("sleep 1", background=True)
|
||||
assert isinstance(handle, CommandHandle)
|
||||
assert handle.pid == 7
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_list(self):
|
||||
respx.get(PROC_URL).respond(200, json={"processes": [{"pid": 1, "tag": "a"}]})
|
||||
procs = await _make_async_commands().list()
|
||||
assert len(procs) == 1
|
||||
assert procs[0].pid == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_async_kill(self):
|
||||
route = respx.delete(f"{PROC_URL}/3").respond(204)
|
||||
await _make_async_commands().kill(3)
|
||||
assert route.called
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_stream(self, monkeypatch):
|
||||
ws = _AsyncFakeWS(
|
||||
[
|
||||
{"type": "start", "pid": 1},
|
||||
{"type": "stdout", "data": "out"},
|
||||
{"type": "exit", "exit_code": 0},
|
||||
]
|
||||
)
|
||||
_patch_async_ws(monkeypatch, ws)
|
||||
events = [e async for e in _make_async_commands().stream("echo out")]
|
||||
assert [e.type for e in events] == ["start", "stdout", "exit"]
|
||||
start = json.loads(ws.sent[0])
|
||||
assert start["cmd"] == "/bin/sh"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_connect(self, monkeypatch):
|
||||
ws = _AsyncFakeWS([{"type": "exit", "exit_code": 0}])
|
||||
_patch_async_ws(monkeypatch, ws)
|
||||
events = [e async for e in _make_async_commands().connect(9)]
|
||||
assert [e.type for e in events] == ["exit"]
|
||||
549
tests/test_filesystem_pty.py
Normal file
549
tests/test_filesystem_pty.py
Normal file
@ -0,0 +1,549 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from wrenn.capsule import Capsule
|
||||
from wrenn.models import FileEntry
|
||||
from wrenn.pty import (
|
||||
AsyncPtySession,
|
||||
PtyEventType,
|
||||
PtySession,
|
||||
_parse_pty_event,
|
||||
)
|
||||
|
||||
BASE = "https://app.wrenn.dev/api"
|
||||
|
||||
|
||||
def _make_capsule(cap_id: str = "cl-abc") -> Capsule:
|
||||
respx.post(f"{BASE}/v1/capsules").respond(
|
||||
201, json={"id": cap_id, "status": "running"}
|
||||
)
|
||||
return Capsule(api_key="wrn_test1234567890abcdef12345678", base_url=BASE)
|
||||
|
||||
|
||||
class TestFilesRead:
|
||||
@respx.mock
|
||||
def test_read_returns_string(self):
|
||||
cap = _make_capsule()
|
||||
content = b"file contents here"
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/read").respond(
|
||||
200, content=content
|
||||
)
|
||||
data = cap.files.read("/app/main.py")
|
||||
assert data == "file contents here"
|
||||
|
||||
@respx.mock
|
||||
def test_read_bytes(self):
|
||||
cap = _make_capsule()
|
||||
content = b"\x00\x01\x02"
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/read").respond(
|
||||
200, content=content
|
||||
)
|
||||
data = cap.files.read_bytes("/bin/binary")
|
||||
assert data == b"\x00\x01\x02"
|
||||
|
||||
|
||||
class TestFilesWrite:
|
||||
@respx.mock
|
||||
def test_write_string(self):
|
||||
cap = _make_capsule()
|
||||
route = respx.post(f"{BASE}/v1/capsules/cl-abc/files/write").respond(204)
|
||||
cap.files.write("/app/main.py", "print('hello')")
|
||||
assert route.called
|
||||
|
||||
@respx.mock
|
||||
def test_write_bytes(self):
|
||||
cap = _make_capsule()
|
||||
route = respx.post(f"{BASE}/v1/capsules/cl-abc/files/write").respond(204)
|
||||
cap.files.write("/app/data.bin", b"\x00\x01\x02")
|
||||
assert route.called
|
||||
|
||||
|
||||
class TestFilesList:
|
||||
@respx.mock
|
||||
def test_list_returns_entries(self):
|
||||
cap = _make_capsule()
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/list").respond(
|
||||
200,
|
||||
json={
|
||||
"entries": [
|
||||
{
|
||||
"name": "main.py",
|
||||
"path": "/home/wrenn-user/main.py",
|
||||
"type": "file",
|
||||
"size": 1024,
|
||||
"mode": 33188,
|
||||
"permissions": "-rw-r--r--",
|
||||
"owner": "wrenn-user",
|
||||
"group": "wrenn-user",
|
||||
"modified_at": 1712899200,
|
||||
"symlink_target": None,
|
||||
},
|
||||
{
|
||||
"name": "config",
|
||||
"path": "/home/wrenn-user/config",
|
||||
"type": "directory",
|
||||
"size": 4096,
|
||||
"mode": 16877,
|
||||
"permissions": "drwxr-xr-x",
|
||||
"owner": "wrenn-user",
|
||||
"group": "wrenn-user",
|
||||
"modified_at": 1712899100,
|
||||
"symlink_target": None,
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
entries = cap.files.list("/home/wrenn-user")
|
||||
assert len(entries) == 2
|
||||
assert isinstance(entries[0], FileEntry)
|
||||
assert entries[0].name == "main.py"
|
||||
assert entries[0].type == "file"
|
||||
assert entries[1].name == "config"
|
||||
assert entries[1].type == "directory"
|
||||
|
||||
@respx.mock
|
||||
def test_list_with_depth(self):
|
||||
cap = _make_capsule()
|
||||
route = respx.post(f"{BASE}/v1/capsules/cl-abc/files/list").respond(
|
||||
200, json={"entries": []}
|
||||
)
|
||||
cap.files.list("/home/wrenn-user", depth=3)
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["depth"] == 3
|
||||
|
||||
@respx.mock
|
||||
def test_list_empty(self):
|
||||
cap = _make_capsule()
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/list").respond(
|
||||
200, json={"entries": []}
|
||||
)
|
||||
entries = cap.files.list("/empty")
|
||||
assert entries == []
|
||||
|
||||
|
||||
class TestFilesMakeDir:
|
||||
@respx.mock
|
||||
def test_make_dir_returns_entry(self):
|
||||
cap = _make_capsule()
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/mkdir").respond(
|
||||
200,
|
||||
json={
|
||||
"entry": {
|
||||
"name": "data",
|
||||
"path": "/home/wrenn-user/data",
|
||||
"type": "directory",
|
||||
"size": 4096,
|
||||
"mode": 16877,
|
||||
"permissions": "drwxr-xr-x",
|
||||
"owner": "wrenn-user",
|
||||
"group": "wrenn-user",
|
||||
"modified_at": 1712899200,
|
||||
"symlink_target": None,
|
||||
}
|
||||
},
|
||||
)
|
||||
entry = cap.files.make_dir("/home/wrenn-user/data")
|
||||
assert isinstance(entry, FileEntry)
|
||||
assert entry.name == "data"
|
||||
assert entry.type == "directory"
|
||||
|
||||
@respx.mock
|
||||
def test_make_dir_existing_returns_gracefully(self):
|
||||
cap = _make_capsule()
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/mkdir").respond(
|
||||
409,
|
||||
json={"error": {"code": "conflict", "message": "already exists"}},
|
||||
)
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/list").respond(
|
||||
200,
|
||||
json={
|
||||
"entries": [
|
||||
{
|
||||
"name": "data",
|
||||
"path": "/home/wrenn-user/data",
|
||||
"type": "directory",
|
||||
"size": 4096,
|
||||
"mode": 16877,
|
||||
"permissions": "drwxr-xr-x",
|
||||
"owner": "wrenn-user",
|
||||
"group": "wrenn-user",
|
||||
"modified_at": 1712899200,
|
||||
"symlink_target": None,
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
entry = cap.files.make_dir("/home/wrenn-user/data")
|
||||
assert entry.name == "data"
|
||||
|
||||
|
||||
class TestFilesRemove:
|
||||
@respx.mock
|
||||
def test_remove_succeeds(self):
|
||||
cap = _make_capsule()
|
||||
route = respx.post(f"{BASE}/v1/capsules/cl-abc/files/remove").respond(204)
|
||||
cap.files.remove("/home/wrenn-user/old_data")
|
||||
assert route.called
|
||||
|
||||
@respx.mock
|
||||
def test_remove_sends_path(self):
|
||||
cap = _make_capsule()
|
||||
route = respx.post(f"{BASE}/v1/capsules/cl-abc/files/remove").respond(204)
|
||||
cap.files.remove("/tmp/test.txt")
|
||||
body = json.loads(route.calls[0].request.content)
|
||||
assert body["path"] == "/tmp/test.txt"
|
||||
|
||||
|
||||
class TestFilesExists:
|
||||
@respx.mock
|
||||
def test_exists_true(self):
|
||||
cap = _make_capsule()
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/list").respond(
|
||||
200,
|
||||
json={
|
||||
"entries": [
|
||||
{"name": "hello.txt", "path": "/tmp/hello.txt", "type": "file"}
|
||||
]
|
||||
},
|
||||
)
|
||||
assert cap.files.exists("/tmp/hello.txt") is True
|
||||
|
||||
@respx.mock
|
||||
def test_exists_false(self):
|
||||
cap = _make_capsule()
|
||||
respx.post(f"{BASE}/v1/capsules/cl-abc/files/list").respond(
|
||||
200, json={"entries": []}
|
||||
)
|
||||
assert cap.files.exists("/tmp/nope.txt") is False
|
||||
|
||||
|
||||
class TestPtyEventParsing:
|
||||
def test_started_event(self):
|
||||
raw = {"type": "started", "tag": "pty-a1b2c3d4", "pid": 42}
|
||||
event = _parse_pty_event(raw)
|
||||
assert event.type == PtyEventType.started
|
||||
assert event.pid == 42
|
||||
assert event.tag == "pty-a1b2c3d4"
|
||||
|
||||
def test_output_event_base64(self):
|
||||
encoded = base64.b64encode(b"ls -la\n").decode()
|
||||
raw = {"type": "output", "data": encoded}
|
||||
event = _parse_pty_event(raw)
|
||||
assert event.type == PtyEventType.output
|
||||
assert event.data == b"ls -la\n"
|
||||
|
||||
def test_output_event_empty(self):
|
||||
raw = {"type": "output", "data": ""}
|
||||
event = _parse_pty_event(raw)
|
||||
assert event.data == b""
|
||||
|
||||
def test_exit_event(self):
|
||||
raw = {"type": "exit", "exit_code": 0}
|
||||
event = _parse_pty_event(raw)
|
||||
assert event.type == PtyEventType.exit
|
||||
assert event.exit_code == 0
|
||||
|
||||
def test_error_event(self):
|
||||
raw = {"type": "error", "data": "process not found", "fatal": True}
|
||||
event = _parse_pty_event(raw)
|
||||
assert event.type == PtyEventType.error
|
||||
assert event.data == "process not found"
|
||||
assert event.fatal is True
|
||||
|
||||
def test_ping_event(self):
|
||||
raw = {"type": "ping"}
|
||||
event = _parse_pty_event(raw)
|
||||
assert event.type == PtyEventType.ping
|
||||
|
||||
|
||||
class TestPtySessionWrite:
|
||||
def test_write_sends_base64_input(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
session.write(b"ls -la\n")
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "input"
|
||||
assert base64.b64decode(sent["data"]) == b"ls -la\n"
|
||||
|
||||
|
||||
class TestPtySessionResize:
|
||||
def test_resize_sends_dimensions(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
session.resize(120, 40)
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "resize"
|
||||
assert sent["cols"] == 120
|
||||
assert sent["rows"] == 40
|
||||
|
||||
def test_resize_zero_raises(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
with pytest.raises(ValueError, match="greater than 0"):
|
||||
session.resize(0, 40)
|
||||
with pytest.raises(ValueError, match="greater than 0"):
|
||||
session.resize(80, 0)
|
||||
|
||||
|
||||
class TestPtySessionKill:
|
||||
def test_kill_sends_message(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
session.kill()
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "kill"
|
||||
|
||||
|
||||
class TestPtySessionIteration:
|
||||
def test_iter_yields_events_until_exit(self):
|
||||
ws = MagicMock()
|
||||
messages = [
|
||||
json.dumps({"type": "started", "tag": "pty-abc12345", "pid": 1}),
|
||||
json.dumps({"type": "output", "data": base64.b64encode(b"hello").decode()}),
|
||||
json.dumps({"type": "exit", "exit_code": 0}),
|
||||
]
|
||||
ws.receive_text.side_effect = messages
|
||||
session = PtySession(ws, "cl-abc")
|
||||
events = list(session)
|
||||
assert len(events) == 3
|
||||
assert events[0].type == PtyEventType.started
|
||||
assert session.tag == "pty-abc12345"
|
||||
assert session.pid == 1
|
||||
assert events[1].type == PtyEventType.output
|
||||
assert events[1].data == b"hello"
|
||||
assert events[2].type == PtyEventType.exit
|
||||
assert events[2].exit_code == 0
|
||||
|
||||
def test_iter_stops_on_fatal_error(self):
|
||||
ws = MagicMock()
|
||||
messages = [
|
||||
json.dumps({"type": "error", "data": "fatal", "fatal": True}),
|
||||
]
|
||||
ws.receive_text.side_effect = messages
|
||||
session = PtySession(ws, "cl-abc")
|
||||
events = list(session)
|
||||
assert len(events) == 1
|
||||
assert events[0].type == PtyEventType.error
|
||||
|
||||
def test_iter_stops_on_disconnect(self):
|
||||
import httpx_ws
|
||||
|
||||
ws = MagicMock()
|
||||
ws.receive_text.side_effect = httpx_ws.WebSocketDisconnect()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
events = list(session)
|
||||
assert events == []
|
||||
|
||||
|
||||
class TestPtySessionPong:
|
||||
def test_ping_triggers_pong(self):
|
||||
ws = MagicMock()
|
||||
ws.receive_text.side_effect = [
|
||||
json.dumps({"type": "ping"}),
|
||||
json.dumps({"type": "exit", "exit_code": 0}),
|
||||
]
|
||||
session = PtySession(ws, "cl-abc")
|
||||
events = list(session)
|
||||
assert events[0].type == PtyEventType.ping
|
||||
sent = [json.loads(c[0][0]) for c in ws.send_text.call_args_list]
|
||||
assert {"type": "pong"} in sent
|
||||
|
||||
def test_no_pong_without_ping(self):
|
||||
ws = MagicMock()
|
||||
ws.receive_text.side_effect = [
|
||||
json.dumps({"type": "output", "data": ""}),
|
||||
json.dumps({"type": "exit", "exit_code": 0}),
|
||||
]
|
||||
session = PtySession(ws, "cl-abc")
|
||||
list(session)
|
||||
sent = [json.loads(c[0][0]) for c in ws.send_text.call_args_list]
|
||||
assert {"type": "pong"} not in sent
|
||||
|
||||
def test_send_pong_swallows_closed_ws(self):
|
||||
import httpx_ws
|
||||
|
||||
ws = MagicMock()
|
||||
ws.send_text.side_effect = httpx_ws.WebSocketNetworkError()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
session._send_pong() # must not raise
|
||||
|
||||
|
||||
class TestPtySessionContextManager:
|
||||
def test_exit_kills_and_closes(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
with session:
|
||||
pass
|
||||
ws.send_text.assert_called()
|
||||
ws.close.assert_called()
|
||||
|
||||
def test_exit_ignores_errors(self):
|
||||
ws = MagicMock()
|
||||
ws.send_text.side_effect = Exception("already closed")
|
||||
session = PtySession(ws, "cl-abc")
|
||||
with session:
|
||||
pass
|
||||
|
||||
|
||||
class TestPtySessionSendStart:
|
||||
def test_send_start_with_defaults(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
session._send_start()
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "start"
|
||||
assert sent["cmd"] == "/bin/bash"
|
||||
assert sent["cols"] == 80
|
||||
assert sent["rows"] == 24
|
||||
|
||||
def test_send_start_with_all_params(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
session._send_start(
|
||||
cmd="/bin/zsh",
|
||||
args=["-l"],
|
||||
cols=120,
|
||||
rows=40,
|
||||
envs={"TERM": "xterm-256color"},
|
||||
cwd="/home/wrenn-user",
|
||||
)
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["cmd"] == "/bin/zsh"
|
||||
assert sent["args"] == ["-l"]
|
||||
assert sent["cols"] == 120
|
||||
|
||||
|
||||
class TestPtySessionSendConnect:
|
||||
def test_send_connect(self):
|
||||
ws = MagicMock()
|
||||
session = PtySession(ws, "cl-abc")
|
||||
session._send_connect("pty-abc12345")
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "connect"
|
||||
assert sent["tag"] == "pty-abc12345"
|
||||
|
||||
|
||||
class TestAsyncPtySession:
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_write_sends_base64(self):
|
||||
ws = AsyncMock()
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
await session.write(b"hello")
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "input"
|
||||
assert base64.b64decode(sent["data"]) == b"hello"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_resize(self):
|
||||
ws = AsyncMock()
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
await session.resize(100, 30)
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "resize"
|
||||
assert sent["cols"] == 100
|
||||
assert sent["rows"] == 30
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_resize_zero_raises(self):
|
||||
ws = AsyncMock()
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
with pytest.raises(ValueError):
|
||||
await session.resize(0, 10)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_kill(self):
|
||||
ws = AsyncMock()
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
await session.kill()
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "kill"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_context_manager(self):
|
||||
ws = AsyncMock()
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
async with session:
|
||||
pass
|
||||
ws.send_text.assert_called()
|
||||
ws.close.assert_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_send_start(self):
|
||||
ws = AsyncMock()
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
await session._send_start(cmd="/bin/zsh", cols=100, rows=30)
|
||||
sent = json.loads(ws.send_text.call_args[0][0])
|
||||
assert sent["type"] == "start"
|
||||
assert sent["cmd"] == "/bin/zsh"
|
||||
assert sent["cols"] == 100
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_ping_triggers_pong(self):
|
||||
ws = AsyncMock()
|
||||
ws.receive_text.side_effect = [
|
||||
json.dumps({"type": "ping"}),
|
||||
json.dumps({"type": "exit", "exit_code": 0}),
|
||||
]
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
events = [e async for e in session]
|
||||
assert events[0].type == PtyEventType.ping
|
||||
sent = [json.loads(c[0][0]) for c in ws.send_text.call_args_list]
|
||||
assert {"type": "pong"} in sent
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_send_pong_swallows_closed_ws(self):
|
||||
import httpx_ws
|
||||
|
||||
ws = AsyncMock()
|
||||
ws.send_text.side_effect = httpx_ws.WebSocketNetworkError()
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
await session._send_pong() # must not raise
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_iteration(self):
|
||||
ws = AsyncMock()
|
||||
messages = [
|
||||
json.dumps({"type": "started", "tag": "pty-xyz", "pid": 5}),
|
||||
json.dumps({"type": "output", "data": base64.b64encode(b"hi").decode()}),
|
||||
json.dumps({"type": "exit", "exit_code": 0}),
|
||||
]
|
||||
ws.receive_text.side_effect = messages
|
||||
session = AsyncPtySession(ws, "cl-abc")
|
||||
events = []
|
||||
async for event in session:
|
||||
events.append(event)
|
||||
assert len(events) == 3
|
||||
assert events[0].type == PtyEventType.started
|
||||
assert session.tag == "pty-xyz"
|
||||
assert session.pid == 5
|
||||
assert events[2].type == PtyEventType.exit
|
||||
|
||||
|
||||
class TestExports:
|
||||
def test_file_entry_importable(self):
|
||||
from wrenn import FileEntry as FE
|
||||
|
||||
assert FE is not None
|
||||
|
||||
def test_pty_session_importable(self):
|
||||
from wrenn import PtySession as PS
|
||||
|
||||
assert PS is not None
|
||||
|
||||
def test_async_pty_session_importable(self):
|
||||
from wrenn import AsyncPtySession as APS
|
||||
|
||||
assert APS is not None
|
||||
|
||||
def test_pty_event_importable(self):
|
||||
from wrenn import PtyEvent as PE
|
||||
from wrenn import PtyEventType as PET
|
||||
|
||||
assert PE is not None
|
||||
assert PET is not None
|
||||
1137
tests/test_git.py
Normal file
1137
tests/test_git.py
Normal file
File diff suppressed because it is too large
Load Diff
408
tests/test_integration.py
Normal file
408
tests/test_integration.py
Normal file
@ -0,0 +1,408 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from wrenn import Capsule, CommandResult
|
||||
from wrenn.commands import CommandHandle, ProcessInfo
|
||||
from wrenn.models import Capsule as CapsuleModel, FileEntry, Status
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
|
||||
_env_loaded = False
|
||||
|
||||
|
||||
def _ensure_env() -> None:
|
||||
global _env_loaded
|
||||
if _env_loaded:
|
||||
return
|
||||
_env_loaded = True
|
||||
env_file = Path(__file__).resolve().parent.parent / ".env"
|
||||
if not env_file.exists():
|
||||
return
|
||||
for line in env_file.read_text().splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, _, value = line.partition("=")
|
||||
key, value = key.strip(), value.strip().strip("\"'")
|
||||
if key and key not in os.environ:
|
||||
os.environ[key] = value
|
||||
|
||||
|
||||
class TestCapsuleLifecycle:
|
||||
"""Each test manages its own capsule to test create/destroy paths."""
|
||||
|
||||
def setup_method(self):
|
||||
_ensure_env()
|
||||
|
||||
def test_create_and_destroy(self):
|
||||
capsule = Capsule()
|
||||
capsule_id = capsule.capsule_id
|
||||
try:
|
||||
assert capsule_id
|
||||
assert capsule.info is not None
|
||||
finally:
|
||||
capsule.destroy(wait=True)
|
||||
|
||||
info = Capsule.get_info(capsule_id)
|
||||
assert info.status in (Status.stopped, Status.missing)
|
||||
|
||||
def test_create_with_wait(self):
|
||||
capsule = Capsule(wait=True)
|
||||
try:
|
||||
assert capsule.info is not None
|
||||
assert capsule.info.status == Status.running
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
def test_context_manager_destroys(self):
|
||||
with Capsule(wait=True) as capsule:
|
||||
capsule_id = capsule.capsule_id
|
||||
assert capsule.is_running()
|
||||
|
||||
info = Capsule.get_info(capsule_id)
|
||||
assert info.status in (Status.stopping, Status.stopped, Status.missing)
|
||||
|
||||
def test_get_info(self):
|
||||
capsule = Capsule(wait=True)
|
||||
try:
|
||||
info = capsule.get_info()
|
||||
assert isinstance(info, CapsuleModel)
|
||||
assert info.id == capsule.capsule_id
|
||||
assert info.status == Status.running
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
def test_pause_and_resume(self):
|
||||
capsule = Capsule(wait=True)
|
||||
try:
|
||||
paused = capsule.pause(wait=True)
|
||||
assert paused.status == Status.paused
|
||||
assert not capsule.is_running()
|
||||
|
||||
resumed = capsule.resume(wait=True)
|
||||
assert resumed.status == Status.running
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
def test_static_destroy(self):
|
||||
capsule = Capsule(wait=True)
|
||||
capsule_id = capsule.capsule_id
|
||||
try:
|
||||
Capsule.destroy(capsule_id, wait=True)
|
||||
except Exception:
|
||||
capsule.destroy()
|
||||
raise
|
||||
|
||||
info = Capsule.get_info(capsule_id)
|
||||
assert info.status in (Status.stopped, Status.missing)
|
||||
|
||||
def test_connect_to_existing(self):
|
||||
capsule = Capsule(wait=True)
|
||||
try:
|
||||
connected = Capsule.connect(capsule.capsule_id)
|
||||
assert connected.capsule_id == capsule.capsule_id
|
||||
assert connected.info is not None
|
||||
assert connected.info.status == Status.running
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
def test_connect_resumes_paused(self):
|
||||
capsule = Capsule(wait=True)
|
||||
try:
|
||||
capsule.pause()
|
||||
connected = Capsule.connect(capsule.capsule_id)
|
||||
assert connected.info is not None
|
||||
assert connected.info.status == Status.running
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
def test_list_capsules(self):
|
||||
capsule = Capsule(wait=True)
|
||||
try:
|
||||
capsules = Capsule.list()
|
||||
assert isinstance(capsules, list)
|
||||
ids = [c.id for c in capsules]
|
||||
assert capsule.capsule_id in ids
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
def test_wait_ready(self):
|
||||
capsule = Capsule()
|
||||
try:
|
||||
capsule.wait_ready(timeout=60)
|
||||
assert capsule.is_running()
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
def test_ping(self):
|
||||
capsule = Capsule(wait=True)
|
||||
try:
|
||||
capsule.ping()
|
||||
finally:
|
||||
capsule.destroy()
|
||||
|
||||
|
||||
class TestCommands:
|
||||
"""Shared capsule for command execution tests."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_run_foreground(self):
|
||||
result = self.capsule.commands.run("echo hello")
|
||||
assert isinstance(result, CommandResult)
|
||||
assert result.exit_code == 0
|
||||
assert "hello" in result.stdout
|
||||
|
||||
def test_run_stderr(self):
|
||||
result = self.capsule.commands.run("echo error >&2")
|
||||
assert "error" in result.stderr
|
||||
|
||||
def test_run_exit_code(self):
|
||||
result = self.capsule.commands.run("exit 42")
|
||||
assert result.exit_code == 42
|
||||
|
||||
def test_run_with_envs(self):
|
||||
result = self.capsule.commands.run("export MY_VAR=test_value && echo $MY_VAR")
|
||||
assert "test_value" in result.stdout
|
||||
|
||||
def test_run_with_cwd(self):
|
||||
result = self.capsule.commands.run("cd /tmp && pwd")
|
||||
assert result.stdout.strip() == "/tmp"
|
||||
|
||||
def test_run_multiline_output(self):
|
||||
result = self.capsule.commands.run("echo -e 'line1\\nline2\\nline3'")
|
||||
assert result.exit_code == 0
|
||||
lines = result.stdout.strip().splitlines()
|
||||
assert len(lines) == 3
|
||||
|
||||
def test_run_background(self):
|
||||
handle = self.capsule.commands.run("sleep 30", background=True, tag="bg-test")
|
||||
assert isinstance(handle, CommandHandle)
|
||||
assert handle.pid > 0
|
||||
assert handle.tag == "bg-test"
|
||||
assert handle.capsule_id == self.capsule.capsule_id
|
||||
|
||||
self.capsule.commands.kill(handle.pid)
|
||||
|
||||
def test_list_processes(self):
|
||||
handle = self.capsule.commands.run("sleep 30", background=True, tag="list-test")
|
||||
try:
|
||||
time.sleep(0.5)
|
||||
processes = self.capsule.commands.list()
|
||||
assert isinstance(processes, list)
|
||||
pids = [p.pid for p in processes]
|
||||
assert handle.pid in pids
|
||||
|
||||
proc = next(p for p in processes if p.pid == handle.pid)
|
||||
assert isinstance(proc, ProcessInfo)
|
||||
finally:
|
||||
self.capsule.commands.kill(handle.pid)
|
||||
|
||||
def test_kill_process(self):
|
||||
handle = self.capsule.commands.run("sleep 30", background=True)
|
||||
self.capsule.commands.kill(handle.pid)
|
||||
# Registry prune runs asynchronously after the process end event,
|
||||
# so poll rather than asserting on a zero-delay list().
|
||||
deadline = time.monotonic() + 5
|
||||
while time.monotonic() < deadline:
|
||||
if handle.pid not in [p.pid for p in self.capsule.commands.list()]:
|
||||
break
|
||||
time.sleep(0.2)
|
||||
assert handle.pid not in [p.pid for p in self.capsule.commands.list()]
|
||||
|
||||
def test_run_duration_ms(self):
|
||||
result = self.capsule.commands.run("sleep 1")
|
||||
assert result.duration_ms is None or result.duration_ms >= 900
|
||||
|
||||
|
||||
class TestFiles:
|
||||
"""Shared capsule for filesystem tests."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_write_and_read(self):
|
||||
self.capsule.files.write("/tmp/test.txt", "hello world")
|
||||
content = self.capsule.files.read("/tmp/test.txt")
|
||||
assert content == "hello world"
|
||||
|
||||
def test_write_and_read_bytes(self):
|
||||
data = b"\x00\x01\x02\xff"
|
||||
self.capsule.files.write("/tmp/test.bin", data)
|
||||
result = self.capsule.files.read_bytes("/tmp/test.bin")
|
||||
assert result == data
|
||||
|
||||
def test_list_directory(self):
|
||||
self.capsule.files.write("/tmp/listdir/a.txt", "a")
|
||||
self.capsule.files.write("/tmp/listdir/b.txt", "b")
|
||||
entries = self.capsule.files.list("/tmp/listdir")
|
||||
assert isinstance(entries, list)
|
||||
names = [e.name for e in entries]
|
||||
assert "a.txt" in names
|
||||
assert "b.txt" in names
|
||||
|
||||
def test_exists(self):
|
||||
self.capsule.files.write("/tmp/exists_test.txt", "x")
|
||||
assert self.capsule.files.exists("/tmp/exists_test.txt")
|
||||
assert not self.capsule.files.exists("/tmp/does_not_exist_xyz.txt")
|
||||
|
||||
def test_make_dir(self):
|
||||
entry = self.capsule.files.make_dir("/tmp/newdir")
|
||||
assert isinstance(entry, FileEntry)
|
||||
assert self.capsule.files.exists("/tmp/newdir")
|
||||
|
||||
def test_make_dir_idempotent(self):
|
||||
self.capsule.files.make_dir("/tmp/idempotent_dir")
|
||||
entry = self.capsule.files.make_dir("/tmp/idempotent_dir")
|
||||
assert isinstance(entry, FileEntry)
|
||||
|
||||
def test_remove_file(self):
|
||||
self.capsule.files.write("/tmp/to_remove.txt", "delete me")
|
||||
assert self.capsule.files.exists("/tmp/to_remove.txt")
|
||||
self.capsule.files.remove("/tmp/to_remove.txt")
|
||||
assert not self.capsule.files.exists("/tmp/to_remove.txt")
|
||||
|
||||
def test_remove_directory(self):
|
||||
self.capsule.files.make_dir("/tmp/dir_to_remove")
|
||||
self.capsule.files.write("/tmp/dir_to_remove/child.txt", "data")
|
||||
self.capsule.files.remove("/tmp/dir_to_remove")
|
||||
assert not self.capsule.files.exists("/tmp/dir_to_remove")
|
||||
|
||||
def test_write_creates_parent_dirs(self):
|
||||
self.capsule.files.write("/tmp/deep/nested/dir/file.txt", "nested")
|
||||
content = self.capsule.files.read("/tmp/deep/nested/dir/file.txt")
|
||||
assert content == "nested"
|
||||
|
||||
def test_list_with_depth(self):
|
||||
self.capsule.files.write("/tmp/depth_test/a/b.txt", "deep")
|
||||
entries_shallow = self.capsule.files.list("/tmp/depth_test", depth=1)
|
||||
entries_deep = self.capsule.files.list("/tmp/depth_test", depth=2)
|
||||
assert len(entries_deep) >= len(entries_shallow)
|
||||
|
||||
def test_overwrite_file(self):
|
||||
self.capsule.files.write("/tmp/overwrite.txt", "original")
|
||||
self.capsule.files.write("/tmp/overwrite.txt", "updated")
|
||||
content = self.capsule.files.read("/tmp/overwrite.txt")
|
||||
assert content == "updated"
|
||||
|
||||
def test_upload_and_download_stream(self):
|
||||
chunks = [b"chunk1", b"chunk2", b"chunk3"]
|
||||
self.capsule.files.upload_stream("/tmp/streamed.bin", iter(chunks))
|
||||
downloaded = b"".join(self.capsule.files.download_stream("/tmp/streamed.bin"))
|
||||
assert downloaded == b"chunk1chunk2chunk3"
|
||||
|
||||
|
||||
class TestGit:
|
||||
"""Shared capsule for git operation tests.
|
||||
|
||||
Initializes a repo at /home/wrenn-user (default cwd) since the exec API
|
||||
does not support the cwd parameter.
|
||||
"""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
cls.capsule.git.init(".", initial_branch="main")
|
||||
cls.capsule.git.configure_user("Test User", "test@example.com")
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_init_created_repo(self):
|
||||
assert self.capsule.files.exists("/home/wrenn-user/.git")
|
||||
|
||||
def test_status_clean(self):
|
||||
status = self.capsule.git.status()
|
||||
assert status.branch == "main"
|
||||
|
||||
def test_add_and_commit(self):
|
||||
self.capsule.files.write("/home/wrenn-user/hello.txt", "hello git")
|
||||
self.capsule.git.add(all=True)
|
||||
result = self.capsule.git.commit("initial commit")
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_status_after_commit(self):
|
||||
status = self.capsule.git.status()
|
||||
assert status.is_clean
|
||||
|
||||
def test_status_with_changes(self):
|
||||
self.capsule.files.write("/home/wrenn-user/dirty.txt", "uncommitted")
|
||||
try:
|
||||
status = self.capsule.git.status()
|
||||
assert not status.is_clean
|
||||
paths = [f.path for f in status.files]
|
||||
assert "dirty.txt" in paths
|
||||
finally:
|
||||
self.capsule.files.remove("/home/wrenn-user/dirty.txt")
|
||||
|
||||
def test_branches(self):
|
||||
branches = self.capsule.git.branches()
|
||||
assert len(branches) >= 1
|
||||
names = [b.name for b in branches]
|
||||
assert "main" in names
|
||||
current = [b for b in branches if b.is_current]
|
||||
assert len(current) == 1
|
||||
|
||||
def test_create_and_checkout_branch(self):
|
||||
self.capsule.git.create_branch("feature-1")
|
||||
branches = self.capsule.git.branches()
|
||||
names = [b.name for b in branches]
|
||||
assert "feature-1" in names
|
||||
|
||||
current = [b for b in branches if b.is_current]
|
||||
assert current[0].name == "feature-1"
|
||||
|
||||
self.capsule.git.checkout_branch("main")
|
||||
|
||||
def test_delete_branch(self):
|
||||
self.capsule.git.create_branch("to-delete")
|
||||
self.capsule.git.checkout_branch("main")
|
||||
self.capsule.git.delete_branch("to-delete")
|
||||
|
||||
branches = self.capsule.git.branches()
|
||||
names = [b.name for b in branches]
|
||||
assert "to-delete" not in names
|
||||
|
||||
def test_set_and_get_config(self):
|
||||
self.capsule.git.set_config("test.key", "test-value")
|
||||
value = self.capsule.git.get_config("test.key")
|
||||
assert value == "test-value"
|
||||
|
||||
def test_get_config_missing_returns_none(self):
|
||||
value = self.capsule.git.get_config("nonexistent.key")
|
||||
assert value is None
|
||||
533
tests/test_integration_advanced.py
Normal file
533
tests/test_integration_advanced.py
Normal file
@ -0,0 +1,533 @@
|
||||
"""Advanced integration tests against a live Wrenn server.
|
||||
|
||||
Skipped automatically when ``WRENN_API_KEY`` is not set (see conftest.py).
|
||||
|
||||
Covers working-directory / environment handling, long-running commands
|
||||
(``apt-get``), interactive PTY sessions, streaming exec, and real ``git``
|
||||
workflows including cloning ``github.com/wrennhq/wrenn``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from wrenn import Capsule
|
||||
from wrenn.commands import StreamExitEvent, StreamStartEvent
|
||||
from wrenn.exceptions import WrennError
|
||||
from wrenn.pty import PtyEventType
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
|
||||
WRENN_REPO = "https://github.com/wrennhq/wrenn"
|
||||
|
||||
_env_loaded = False
|
||||
|
||||
|
||||
def _ensure_env() -> None:
|
||||
global _env_loaded
|
||||
if _env_loaded:
|
||||
return
|
||||
_env_loaded = True
|
||||
env_file = Path(__file__).resolve().parent.parent / ".env"
|
||||
if not env_file.exists():
|
||||
return
|
||||
for line in env_file.read_text().splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, _, value = line.partition("=")
|
||||
key, value = key.strip(), value.strip().strip("\"'")
|
||||
if key and key not in os.environ:
|
||||
os.environ[key] = value
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
# Working directory & environment
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestCommandEnvironment:
|
||||
"""cwd / envs handling for foreground commands."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_cwd_changes_working_directory(self):
|
||||
result = self.capsule.commands.run("pwd", cwd="/tmp")
|
||||
assert result.exit_code == 0
|
||||
assert result.stdout.strip() == "/tmp"
|
||||
|
||||
def test_default_cwd_is_home(self):
|
||||
result = self.capsule.commands.run("pwd")
|
||||
assert result.stdout.strip() == "/home/wrenn-user"
|
||||
|
||||
def test_cwd_resolves_relative_paths(self):
|
||||
self.capsule.files.make_dir("/tmp/cwd_probe/sub")
|
||||
result = self.capsule.commands.run("ls", cwd="/tmp/cwd_probe")
|
||||
assert "sub" in result.stdout
|
||||
|
||||
def test_cwd_nonexistent_raises(self):
|
||||
with pytest.raises(WrennError):
|
||||
self.capsule.commands.run("pwd", cwd="/no/such/dir/xyz")
|
||||
|
||||
def test_cwd_does_not_persist_between_calls(self):
|
||||
# Each run is a fresh process — `cd` in one does not affect the next.
|
||||
self.capsule.commands.run("cd /tmp")
|
||||
result = self.capsule.commands.run("pwd")
|
||||
assert result.stdout.strip() == "/home/wrenn-user"
|
||||
|
||||
def test_single_env_var(self):
|
||||
result = self.capsule.commands.run("echo $GREETING", envs={"GREETING": "hi"})
|
||||
assert result.stdout.strip() == "hi"
|
||||
|
||||
def test_multiple_env_vars(self):
|
||||
result = self.capsule.commands.run(
|
||||
"echo $A-$B-$C", envs={"A": "1", "B": "2", "C": "3"}
|
||||
)
|
||||
assert result.stdout.strip() == "1-2-3"
|
||||
|
||||
def test_env_vars_do_not_leak_between_calls(self):
|
||||
self.capsule.commands.run("echo $SECRET", envs={"SECRET": "leaky"})
|
||||
result = self.capsule.commands.run("echo [$SECRET]")
|
||||
assert result.stdout.strip() == "[]"
|
||||
|
||||
def test_env_var_with_special_chars(self):
|
||||
value = "a b&c|d;e"
|
||||
result = self.capsule.commands.run('printf "%s" "$X"', envs={"X": value})
|
||||
assert result.stdout == value
|
||||
|
||||
def test_base_environment_present(self):
|
||||
result = self.capsule.commands.run("echo $HOME; echo $PATH")
|
||||
lines = result.stdout.strip().splitlines()
|
||||
assert lines[0] == "/home/wrenn-user"
|
||||
assert "/usr/bin" in lines[1]
|
||||
|
||||
def test_sudo_available(self):
|
||||
result = self.capsule.commands.run("which sudo")
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_sudo_runs_without_password(self):
|
||||
result = self.capsule.commands.run("sudo whoami")
|
||||
assert result.exit_code == 0
|
||||
assert result.stdout.strip() == "root"
|
||||
|
||||
def test_sudo_can_write_to_protected_path(self):
|
||||
result = self.capsule.commands.run(
|
||||
"sudo touch /opt/sudo-test-marker && cat /opt/sudo-test-marker"
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_sudo_can_read_root_owned_file(self):
|
||||
result = self.capsule.commands.run("sudo cat /etc/shadow | head -1")
|
||||
assert result.exit_code == 0
|
||||
assert "root" in result.stdout
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
# Long-running commands
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestLongRunningCommands:
|
||||
"""apt-get installs and other slow commands."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_apt_get_install(self):
|
||||
result = self.capsule.commands.run(
|
||||
"sudo apt-get update -qq && sudo apt-get install -y -qq cowsay", timeout=300
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_apt_installed_binary_runs(self):
|
||||
# Depends on test_apt_get_install having installed the package.
|
||||
self.capsule.commands.run("apt-get install -y -qq cowsay", timeout=300)
|
||||
result = self.capsule.commands.run("/usr/games/cowsay moo")
|
||||
assert result.exit_code == 0
|
||||
assert "moo" in result.stdout
|
||||
|
||||
def test_foreground_timeout_raises(self):
|
||||
# A command exceeding its timeout surfaces as a server-side error.
|
||||
with pytest.raises(WrennError):
|
||||
self.capsule.commands.run("sleep 20", timeout=2)
|
||||
|
||||
def test_long_sleep_in_background_returns_immediately(self):
|
||||
start = time.monotonic()
|
||||
handle = self.capsule.commands.run(
|
||||
"sleep 60", background=True, tag="long-sleep"
|
||||
)
|
||||
elapsed = time.monotonic() - start
|
||||
assert elapsed < 10
|
||||
assert handle.pid > 0
|
||||
self.capsule.commands.kill(handle.pid)
|
||||
|
||||
def test_slow_command_within_timeout(self):
|
||||
result = self.capsule.commands.run("sleep 3 && echo done", timeout=30)
|
||||
assert result.exit_code == 0
|
||||
assert result.stdout.strip() == "done"
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
# PTY sessions
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
def _drain_pty(term, *, max_events: int = 200) -> tuple[bytes, int | None]:
|
||||
"""Collect PTY output until exit; return (output, exit_code)."""
|
||||
output = b""
|
||||
exit_code: int | None = None
|
||||
for i, event in enumerate(term):
|
||||
if event.type == PtyEventType.output and event.data:
|
||||
output += event.data
|
||||
elif event.type == PtyEventType.exit:
|
||||
exit_code = event.exit_code
|
||||
break
|
||||
elif event.type == PtyEventType.error and event.fatal:
|
||||
break
|
||||
if i >= max_events:
|
||||
break
|
||||
return output, exit_code
|
||||
|
||||
|
||||
class TestPty:
|
||||
"""Interactive PTY behaviour."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_pty_runs_command_and_exits(self):
|
||||
with self.capsule.pty(cmd="/bin/bash") as term:
|
||||
term.write(b"echo pty-result-$((6*7))\n")
|
||||
term.write(b"exit\n")
|
||||
output, exit_code = _drain_pty(term)
|
||||
assert b"pty-result-42" in output
|
||||
assert exit_code is not None
|
||||
|
||||
def test_pty_started_event_sets_tag_and_pid(self):
|
||||
with self.capsule.pty(cmd="/bin/bash") as term:
|
||||
term.write(b"exit\n")
|
||||
_drain_pty(term)
|
||||
assert term.tag is not None
|
||||
assert term.tag.startswith("pty-")
|
||||
assert term.pid is not None and term.pid > 0
|
||||
|
||||
def test_pty_respects_cwd(self):
|
||||
with self.capsule.pty(cmd="/bin/bash", cwd="/tmp") as term:
|
||||
term.write(b"pwd\n")
|
||||
term.write(b"exit\n")
|
||||
output, _ = _drain_pty(term)
|
||||
assert b"/tmp" in output
|
||||
|
||||
def test_pty_respects_envs(self):
|
||||
with self.capsule.pty(cmd="/bin/bash", envs={"PTY_VAR": "xyzzy"}) as term:
|
||||
term.write(b"echo marker-$PTY_VAR\n")
|
||||
term.write(b"exit\n")
|
||||
output, _ = _drain_pty(term)
|
||||
assert b"marker-xyzzy" in output
|
||||
|
||||
def test_pty_resize(self):
|
||||
with self.capsule.pty(cmd="/bin/bash", cols=80, rows=24) as term:
|
||||
term.resize(120, 40)
|
||||
term.write(b"echo resized\n")
|
||||
term.write(b"exit\n")
|
||||
output, _ = _drain_pty(term)
|
||||
assert b"resized" in output
|
||||
|
||||
def test_pty_explicit_command(self):
|
||||
with self.capsule.pty(cmd="/bin/echo", args=["hello-from-argv"]) as term:
|
||||
output, exit_code = _drain_pty(term)
|
||||
assert b"hello-from-argv" in output
|
||||
|
||||
def test_pty_exit_code_nonzero(self):
|
||||
with self.capsule.pty(cmd="/bin/bash") as term:
|
||||
term.write(b"exit 3\n")
|
||||
_, exit_code = _drain_pty(term)
|
||||
assert exit_code == 3
|
||||
|
||||
def test_pty_survives_idle_ping_cycle(self):
|
||||
# The server emits a keepalive `ping` (~every 30s); the SDK must
|
||||
# auto-reply `pong` and the session must stay usable afterwards.
|
||||
with self.capsule.pty(cmd="/bin/bash") as term:
|
||||
saw_ping = False
|
||||
for event in term:
|
||||
if event.type == PtyEventType.ping:
|
||||
saw_ping = True
|
||||
break
|
||||
if event.type == PtyEventType.exit:
|
||||
break
|
||||
if event.type == PtyEventType.error and event.fatal:
|
||||
break
|
||||
assert saw_ping, "no keepalive ping received"
|
||||
term.write(b"echo still-alive\n")
|
||||
term.write(b"exit\n")
|
||||
output, _ = _drain_pty(term)
|
||||
assert b"still-alive" in output
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
# Streaming exec
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestStreamingExec:
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_stream_emits_start_and_exit(self):
|
||||
events = list(self.capsule.commands.stream("echo streamed"))
|
||||
types = [e.type for e in events]
|
||||
assert "exit" in types
|
||||
starts = [e for e in events if isinstance(e, StreamStartEvent)]
|
||||
exits = [e for e in events if isinstance(e, StreamExitEvent)]
|
||||
assert exits and exits[0].exit_code == 0
|
||||
if starts:
|
||||
assert starts[0].pid > 0
|
||||
|
||||
def test_stream_captures_stdout(self):
|
||||
events = list(self.capsule.commands.stream("for i in 1 2 3; do echo n$i; done"))
|
||||
out = "".join(
|
||||
e.data for e in events if e.type == "stdout" and getattr(e, "data", None)
|
||||
)
|
||||
assert "n1" in out and "n3" in out
|
||||
|
||||
def test_stream_nonzero_exit(self):
|
||||
events = list(self.capsule.commands.stream("exit 5"))
|
||||
exits = [e for e in events if isinstance(e, StreamExitEvent)]
|
||||
assert exits and exits[0].exit_code == 5
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
# Process connect — attach to a background process over WebSocket
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestProcessConnect:
|
||||
"""commands.connect — must survive the server's abrupt WebSocket close."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_connect_streams_running_process(self):
|
||||
handle = self.capsule.commands.run(
|
||||
"for i in $(seq 1 5); do echo tick$i; sleep 1; done",
|
||||
background=True,
|
||||
tag="connect-run",
|
||||
)
|
||||
time.sleep(0.3)
|
||||
events = list(self.capsule.commands.connect(handle.pid))
|
||||
types = [e.type for e in events]
|
||||
assert "exit" in types
|
||||
# connect streams output from the attach point onward, so early
|
||||
# ticks may be missed — assert it captured the live tail.
|
||||
out = "".join(
|
||||
e.data for e in events if e.type == "stdout" and getattr(e, "data", None)
|
||||
)
|
||||
assert "tick" in out
|
||||
|
||||
def test_connect_to_finished_process_does_not_raise(self):
|
||||
handle = self.capsule.commands.run("echo quick", background=True)
|
||||
time.sleep(2)
|
||||
# Process already exited — server closes the WebSocket abruptly;
|
||||
# the iterator must terminate cleanly rather than raise.
|
||||
events = list(self.capsule.commands.connect(handle.pid))
|
||||
assert isinstance(events, list)
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
# Git — real workflows including cloning wrennhq/wrenn
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestGitClone:
|
||||
"""Clone github.com/wrennhq/wrenn and operate on it."""
|
||||
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
cls.capsule.git.clone(
|
||||
WRENN_REPO, "/home/wrenn-user/wrenn", depth=1, timeout=300
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_clone_created_repo(self):
|
||||
assert self.capsule.files.exists("/home/wrenn-user/wrenn/.git")
|
||||
|
||||
def test_clone_checked_out_files(self):
|
||||
entries = self.capsule.files.list("/home/wrenn-user/wrenn")
|
||||
names = [e.name for e in entries]
|
||||
assert "README.md" in names
|
||||
|
||||
def test_status_of_clone_is_clean(self):
|
||||
status = self.capsule.git.status(cwd="/home/wrenn-user/wrenn")
|
||||
assert status.branch == "main"
|
||||
assert status.is_clean
|
||||
|
||||
def test_branches_lists_main(self):
|
||||
branches = self.capsule.git.branches(cwd="/home/wrenn-user/wrenn")
|
||||
names = [b.name for b in branches]
|
||||
assert "main" in names
|
||||
assert any(b.is_current for b in branches)
|
||||
|
||||
def test_remote_get_origin(self):
|
||||
url = self.capsule.git.remote_get("origin", cwd="/home/wrenn-user/wrenn")
|
||||
assert url is not None
|
||||
assert "wrennhq/wrenn" in url
|
||||
|
||||
def test_git_log_has_commit(self):
|
||||
result = self.capsule.commands.run(
|
||||
"git log --oneline -1", cwd="/home/wrenn-user/wrenn"
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert result.stdout.strip()
|
||||
|
||||
def test_modify_add_commit(self):
|
||||
marker = uuid.uuid4().hex
|
||||
self.capsule.git.configure_user(
|
||||
"CI Bot", "ci@example.com", cwd="/home/wrenn-user/wrenn", scope="local"
|
||||
)
|
||||
self.capsule.files.write(
|
||||
f"/home/wrenn-user/wrenn/sdk_probe_{marker}.txt", marker
|
||||
)
|
||||
self.capsule.git.add([f"sdk_probe_{marker}.txt"], cwd="/home/wrenn-user/wrenn")
|
||||
|
||||
staged = self.capsule.git.status(cwd="/home/wrenn-user/wrenn")
|
||||
assert staged.has_staged
|
||||
|
||||
result = self.capsule.git.commit("probe commit", cwd="/home/wrenn-user/wrenn")
|
||||
assert result.exit_code == 0
|
||||
|
||||
after = self.capsule.git.status(cwd="/home/wrenn-user/wrenn")
|
||||
assert after.is_clean
|
||||
assert after.ahead >= 1
|
||||
|
||||
def test_create_and_checkout_branch_in_clone(self):
|
||||
self.capsule.git.create_branch("sdk-feature", cwd="/home/wrenn-user/wrenn")
|
||||
branches = self.capsule.git.branches(cwd="/home/wrenn-user/wrenn")
|
||||
current = [b for b in branches if b.is_current]
|
||||
assert current and current[0].name == "sdk-feature"
|
||||
self.capsule.git.checkout_branch("main", cwd="/home/wrenn-user/wrenn")
|
||||
|
||||
def test_diff_via_commands(self):
|
||||
self.capsule.files.write("/home/wrenn-user/wrenn/README.md", "overwritten\n")
|
||||
try:
|
||||
result = self.capsule.commands.run(
|
||||
"git diff --stat", cwd="/home/wrenn-user/wrenn"
|
||||
)
|
||||
assert "README.md" in result.stdout
|
||||
finally:
|
||||
self.capsule.git.restore(
|
||||
["README.md"], worktree=True, cwd="/home/wrenn-user/wrenn"
|
||||
)
|
||||
|
||||
|
||||
class TestGitErrors:
|
||||
capsule: Capsule
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
_ensure_env()
|
||||
cls.capsule = Capsule(wait=True)
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
try:
|
||||
cls.capsule.destroy()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_clone_nonexistent_repo_raises(self):
|
||||
from wrenn._git import GitError
|
||||
|
||||
with pytest.raises(GitError):
|
||||
self.capsule.git.clone(
|
||||
"https://github.com/wrennhq/this-repo-does-not-exist-xyz",
|
||||
"/home/wrenn-user/missing",
|
||||
timeout=120,
|
||||
)
|
||||
|
||||
def test_status_outside_repo_raises(self):
|
||||
from wrenn._git import GitError
|
||||
|
||||
with pytest.raises(GitError):
|
||||
self.capsule.git.status(cwd="/tmp")
|
||||
|
||||
def test_clone_with_branch(self):
|
||||
self.capsule.git.clone(
|
||||
WRENN_REPO,
|
||||
"/home/wrenn-user/wrenn-main",
|
||||
branch="main",
|
||||
depth=1,
|
||||
timeout=300,
|
||||
)
|
||||
status = self.capsule.git.status(cwd="/home/wrenn-user/wrenn-main")
|
||||
assert status.branch == "main"
|
||||
Reference in New Issue
Block a user