wspx 0.1.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wspx/__init__.py +24 -0
- wspx/_version.py +1 -0
- wspx/addressing.py +415 -0
- wspx/auth.py +77 -0
- wspx/cli/__init__.py +1 -0
- wspx/cli/client.py +124 -0
- wspx/cli/config.py +89 -0
- wspx/cli/connect.py +88 -0
- wspx/cli/main.py +848 -0
- wspx/cloud/__init__.py +20 -0
- wspx/cloud/app.py +191 -0
- wspx/cloud/backend.py +141 -0
- wspx/cloud/compose.py +85 -0
- wspx/cloud/deploy.py +102 -0
- wspx/config.py +189 -0
- wspx/control/__init__.py +5 -0
- wspx/control/app.py +488 -0
- wspx/control/backend.py +244 -0
- wspx/control/gateway_client.py +79 -0
- wspx/control/mcp_server.py +71 -0
- wspx/control/sessions.py +424 -0
- wspx/control/tracedb.py +183 -0
- wspx/control/ui/assets/index-BJFweZAn.css +1 -0
- wspx/control/ui/assets/index-DhUVhWIK.js +10 -0
- wspx/control/ui/index.html +13 -0
- wspx/control/views.py +180 -0
- wspx/diagnostics.py +146 -0
- wspx/driver/__init__.py +7 -0
- wspx/driver/loop.py +255 -0
- wspx/driver/messages.py +62 -0
- wspx/driver/providers/__init__.py +84 -0
- wspx/driver/providers/anthropic.py +149 -0
- wspx/driver/providers/compat.py +115 -0
- wspx/driver/providers/google.py +138 -0
- wspx/driver/providers/openai.py +125 -0
- wspx/driver/run.py +134 -0
- wspx/errors.py +69 -0
- wspx/gateway/__init__.py +8 -0
- wspx/gateway/app.py +626 -0
- wspx/gateway/ledger.py +536 -0
- wspx/gateway/prices.py +75 -0
- wspx/gateway/usage.py +284 -0
- wspx/handles.py +117 -0
- wspx/integrations/AGENTS.snippet.md +8 -0
- wspx/integrations/SKILL.md +52 -0
- wspx/integrations/__init__.py +17 -0
- wspx/integrations/claude-plugin/.claude-plugin/plugin.json +30 -0
- wspx/integrations/claude-plugin/skills/wspx/SKILL.md +52 -0
- wspx/integrations/codex-hooks.toml +9 -0
- wspx/lab/__init__.py +5 -0
- wspx/lab/analysis.py +485 -0
- wspx/lab/check.py +489 -0
- wspx/lab/evaluate.py +108 -0
- wspx/lab/export.py +275 -0
- wspx/lab/grep.py +754 -0
- wspx/lab/index.py +239 -0
- wspx/lab/ir/__init__.py +1 -0
- wspx/lab/ir/capture.py +299 -0
- wspx/lab/ir/graph.py +148 -0
- wspx/lab/ir/model_ir.py +522 -0
- wspx/lab/ir/semantics.py +1255 -0
- wspx/lab/ir/tape.py +853 -0
- wspx/lab/loader.py +204 -0
- wspx/lab/merge.py +200 -0
- wspx/lab/ops/__init__.py +76 -0
- wspx/lab/ops/builtin.py +379 -0
- wspx/lab/patch.py +320 -0
- wspx/lab/probe.py +206 -0
- wspx/lab/results.py +114 -0
- wspx/lab/sdk.py +120 -0
- wspx/lab/server.py +236 -0
- wspx/lab/session.py +1574 -0
- wspx/lab/tensors.py +119 -0
- wspx/lab/text.py +69 -0
- wspx/lab/tree.py +302 -0
- wspx/loops.py +127 -0
- wspx/project.py +143 -0
- wspx/py.typed +0 -0
- wspx/safety/__init__.py +15 -0
- wspx/safety/kill.py +47 -0
- wspx/safety/sweeper.py +68 -0
- wspx/store/__init__.py +64 -0
- wspx/store/objects.py +130 -0
- wspx/store/overlay.py +209 -0
- wspx/store/refs.py +150 -0
- wspx/testing/__init__.py +21 -0
- wspx/testing/api.py +155 -0
- wspx/testing/builtin.py +139 -0
- wspx/testing/runner.py +257 -0
- wspx/tools/__init__.py +17 -0
- wspx/tools/dispatch.py +129 -0
- wspx/tools/schemas.py +313 -0
- wspx/vcs/__init__.py +17 -0
- wspx/vcs/repo.py +431 -0
- wspx-0.1.0.dev0.dist-info/METADATA +278 -0
- wspx-0.1.0.dev0.dist-info/RECORD +99 -0
- wspx-0.1.0.dev0.dist-info/WHEEL +4 -0
- wspx-0.1.0.dev0.dist-info/entry_points.txt +10 -0
- wspx-0.1.0.dev0.dist-info/licenses/LICENSE +201 -0
wspx/cloud/__init__.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""wspx on Modal: the app, its images and the backend that runs labs as sandboxes (§5.12).
|
|
2
|
+
|
|
3
|
+
Everything is deployed into the user's own account, in a dedicated Modal
|
|
4
|
+
environment (``wspx``), so a Modal environment budget can cap it if wanted.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
APP_NAME = "wspx"
|
|
8
|
+
ENVIRONMENT = "wspx"
|
|
9
|
+
MODELS_VOLUME = "wspx-models"
|
|
10
|
+
STORE_VOLUME = "wspx-store"
|
|
11
|
+
LEDGER_VOLUME = "wspx-ledger"
|
|
12
|
+
CONTROL_SECRET = "wspx-control"
|
|
13
|
+
KEY_SECRETS = {
|
|
14
|
+
"anthropic": ("wspx-key-anthropic", "ANTHROPIC_API_KEY"),
|
|
15
|
+
"openai": ("wspx-key-openai", "OPENAI_API_KEY"),
|
|
16
|
+
"google": ("wspx-key-google", "GEMINI_API_KEY"),
|
|
17
|
+
"hf": ("wspx-key-hf", "HF_TOKEN"),
|
|
18
|
+
}
|
|
19
|
+
"""One secret per provider, so setting one key never touches the others."""
|
|
20
|
+
LAB_PORT = 8080
|
wspx/cloud/app.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""The Modal app. ``wspx deploy`` deploys it programmatically with ``app.deploy()``.
|
|
2
|
+
|
|
3
|
+
================= ======================================================
|
|
4
|
+
control CPU, scales to zero: API, UI, MCP, store, scheduler
|
|
5
|
+
gateway CPU, one instance: provider keys, metering, limits
|
|
6
|
+
runner CPU, one call per session: the agent loop, no keys
|
|
7
|
+
download CPU: fetches weights and model code into wspx-models
|
|
8
|
+
sweeper CPU, every 5 minutes: terminates unleased sandboxes
|
|
9
|
+
labs GPU sandboxes created by the control plane at runtime
|
|
10
|
+
================= ======================================================
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import importlib.metadata
|
|
16
|
+
import json
|
|
17
|
+
import os
|
|
18
|
+
import re
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
import modal
|
|
22
|
+
|
|
23
|
+
from wspx import __version__
|
|
24
|
+
from wspx.cloud import (
|
|
25
|
+
APP_NAME,
|
|
26
|
+
CONTROL_SECRET,
|
|
27
|
+
ENVIRONMENT,
|
|
28
|
+
KEY_SECRETS,
|
|
29
|
+
LEDGER_VOLUME,
|
|
30
|
+
MODELS_VOLUME,
|
|
31
|
+
STORE_VOLUME,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
app = modal.App(APP_NAME)
|
|
35
|
+
|
|
36
|
+
models_volume = modal.Volume.from_name(
|
|
37
|
+
MODELS_VOLUME, create_if_missing=True, environment_name=ENVIRONMENT
|
|
38
|
+
)
|
|
39
|
+
store_volume = modal.Volume.from_name(
|
|
40
|
+
STORE_VOLUME, create_if_missing=True, environment_name=ENVIRONMENT
|
|
41
|
+
)
|
|
42
|
+
ledger_volume = modal.Volume.from_name(
|
|
43
|
+
LEDGER_VOLUME, create_if_missing=True, environment_name=ENVIRONMENT
|
|
44
|
+
)
|
|
45
|
+
control_secret = modal.Secret.from_name(CONTROL_SECRET, environment_name=ENVIRONMENT)
|
|
46
|
+
key_secrets = [
|
|
47
|
+
modal.Secret.from_name(name, environment_name=ENVIRONMENT) for name, _ in KEY_SECRETS.values()
|
|
48
|
+
]
|
|
49
|
+
hf_secret = modal.Secret.from_name(KEY_SECRETS["hf"][0], environment_name=ENVIRONMENT)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
_DEV_REQUIRES = "WSPX_DEV_REQUIRES"
|
|
53
|
+
_SOURCE_IGNORE = ["**/__pycache__/**", "**/*.pyc"]
|
|
54
|
+
_EXTRA_MARKER = re.compile(r"""\bextra\s*==\s*["']([^"']+)["']""")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _source_requires() -> list[str] | None:
|
|
58
|
+
"""wspx's requirements when it runs from a source checkout; None for a PyPI install.
|
|
59
|
+
|
|
60
|
+
Locally this reads the installed distribution. Inside a container the
|
|
61
|
+
source-built images carry the list in their environment, because copied
|
|
62
|
+
source has no distribution metadata; the control plane needs it to rebuild
|
|
63
|
+
the lab image definition (which Modal then finds in its image cache).
|
|
64
|
+
"""
|
|
65
|
+
if not modal.is_local():
|
|
66
|
+
raw = os.environ.get(_DEV_REQUIRES)
|
|
67
|
+
return json.loads(raw) if raw else None
|
|
68
|
+
dist = importlib.metadata.distribution("wspx")
|
|
69
|
+
direct = dist.read_text("direct_url.json") or ""
|
|
70
|
+
if os.environ.get("WSPX_DEV_SOURCE") or '"editable": true' in direct or "file://" in direct:
|
|
71
|
+
return list(dist.requires or [])
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _deps_for(requires: list[str], extra: str) -> list[str]:
|
|
76
|
+
"""The base requirements plus one extra's, markers stripped (images are all Linux).
|
|
77
|
+
|
|
78
|
+
Metadata writers quote the marker either way (``extra == 'lab'`` from
|
|
79
|
+
hatchling), so the extra is parsed rather than string-matched.
|
|
80
|
+
"""
|
|
81
|
+
deps = []
|
|
82
|
+
for req in requires:
|
|
83
|
+
spec, _, marker = req.partition(";")
|
|
84
|
+
match = _EXTRA_MARKER.search(marker)
|
|
85
|
+
if match is None or match.group(1) == extra:
|
|
86
|
+
deps.append(spec.strip())
|
|
87
|
+
return deps
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _image(extra: str) -> modal.Image:
|
|
91
|
+
"""The same wspx version as the CLI, with the extras that role runs.
|
|
92
|
+
|
|
93
|
+
A wspx installed from a source checkout (development) is copied in,
|
|
94
|
+
with its data files (the UI bundle, the skill), instead of fetched from PyPI.
|
|
95
|
+
"""
|
|
96
|
+
base = modal.Image.debian_slim(python_version="3.12")
|
|
97
|
+
requires = _source_requires()
|
|
98
|
+
if requires is None:
|
|
99
|
+
return base.uv_pip_install(f"wspx[{extra}]=={__version__}")
|
|
100
|
+
deps = _deps_for(requires, extra)
|
|
101
|
+
image = base.env({_DEV_REQUIRES: json.dumps(requires)})
|
|
102
|
+
image = image.uv_pip_install(*deps) if deps else image
|
|
103
|
+
return image.add_local_python_source("wspx", copy=True, ignore=_SOURCE_IGNORE)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
control_image = _image("control")
|
|
107
|
+
gateway_image = _image("gateway")
|
|
108
|
+
driver_image = _image("driver")
|
|
109
|
+
lab_image = _image("lab")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@app.function(
|
|
113
|
+
image=control_image,
|
|
114
|
+
volumes={"/store": store_volume},
|
|
115
|
+
secrets=[control_secret],
|
|
116
|
+
max_containers=1,
|
|
117
|
+
scaledown_window=300,
|
|
118
|
+
timeout=24 * 3600,
|
|
119
|
+
)
|
|
120
|
+
@modal.concurrent(max_inputs=64)
|
|
121
|
+
@modal.asgi_app()
|
|
122
|
+
def control() -> object:
|
|
123
|
+
from wspx.cloud.backend import ModalBackend
|
|
124
|
+
from wspx.control.app import ControlSettings, create_app
|
|
125
|
+
|
|
126
|
+
settings = ControlSettings.from_env()
|
|
127
|
+
settings.public_url = settings.public_url or control.get_web_url() or ""
|
|
128
|
+
settings.gateway_url = settings.gateway_url or gateway.get_web_url() or ""
|
|
129
|
+
backend = ModalBackend(store_volume=store_volume)
|
|
130
|
+
return create_app(settings, backend)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@app.function(
|
|
134
|
+
image=gateway_image,
|
|
135
|
+
volumes={"/ledger": ledger_volume},
|
|
136
|
+
secrets=[control_secret, *key_secrets],
|
|
137
|
+
max_containers=1,
|
|
138
|
+
min_containers=0,
|
|
139
|
+
scaledown_window=600,
|
|
140
|
+
timeout=24 * 3600,
|
|
141
|
+
)
|
|
142
|
+
@modal.concurrent(max_inputs=128)
|
|
143
|
+
@modal.asgi_app()
|
|
144
|
+
def gateway() -> object:
|
|
145
|
+
from wspx.gateway.app import GatewayConfig, create_app
|
|
146
|
+
|
|
147
|
+
return create_app(GatewayConfig.from_env())
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
@app.function(image=driver_image, timeout=24 * 3600, restrict_modal_access=True)
|
|
151
|
+
def runner(env: dict[str, str]) -> str:
|
|
152
|
+
"""One session's agent loop. It receives tokens, never provider keys."""
|
|
153
|
+
from wspx.driver.run import run_from_env
|
|
154
|
+
|
|
155
|
+
result = run_from_env(env)
|
|
156
|
+
return f"{result.status}: {result.reason}"
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@app.function(
|
|
160
|
+
image=lab_image, volumes={"/models": models_volume}, secrets=[hf_secret], timeout=6 * 3600
|
|
161
|
+
)
|
|
162
|
+
def download(model: str, revision: str | None = None) -> str:
|
|
163
|
+
"""Fetch weights and modeling code into the models volume; returns the directory."""
|
|
164
|
+
from huggingface_hub import snapshot_download
|
|
165
|
+
|
|
166
|
+
target = Path("/models") / (model.replace("/", "--") + (f"@{revision}" if revision else ""))
|
|
167
|
+
if not (target / "config.json").exists():
|
|
168
|
+
snapshot_download(
|
|
169
|
+
model,
|
|
170
|
+
revision=revision,
|
|
171
|
+
local_dir=str(target),
|
|
172
|
+
allow_patterns=[
|
|
173
|
+
"*.json",
|
|
174
|
+
"*.safetensors",
|
|
175
|
+
"*.py",
|
|
176
|
+
"*.model",
|
|
177
|
+
"*.txt",
|
|
178
|
+
"*.jinja",
|
|
179
|
+
"*.tiktoken",
|
|
180
|
+
],
|
|
181
|
+
)
|
|
182
|
+
models_volume.commit()
|
|
183
|
+
return str(target)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@app.function(image=control_image, schedule=modal.Period(minutes=5), timeout=300)
|
|
187
|
+
def sweeper() -> dict[str, object]:
|
|
188
|
+
from wspx.safety.sweeper import modal_sweep
|
|
189
|
+
|
|
190
|
+
report = modal_sweep(app_name=APP_NAME, environment=ENVIRONMENT)
|
|
191
|
+
return {"checked": report.checked, "terminated": report.terminated}
|
wspx/cloud/backend.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""Labs as Modal Sandboxes; runners as Modal function calls (§3.2, §5.12)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import secrets
|
|
6
|
+
import time
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
from wspx.cloud import APP_NAME, ENVIRONMENT, LAB_PORT
|
|
12
|
+
from wspx.control.backend import HttpLab, SessionRecord
|
|
13
|
+
from wspx.safety import LEASE_DICT, TAG_KEY, TAG_VALUE
|
|
14
|
+
|
|
15
|
+
READY_TIMEOUT_S = 3600.0
|
|
16
|
+
SANDBOX_MAX_S = 24 * 3600
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ModalRunner:
|
|
20
|
+
def __init__(self, call: Any) -> None:
|
|
21
|
+
self.call = call
|
|
22
|
+
|
|
23
|
+
def alive(self) -> bool:
|
|
24
|
+
try:
|
|
25
|
+
self.call.get(timeout=0)
|
|
26
|
+
except TimeoutError:
|
|
27
|
+
return True
|
|
28
|
+
except Exception:
|
|
29
|
+
return False
|
|
30
|
+
return False
|
|
31
|
+
|
|
32
|
+
def stop(self) -> None:
|
|
33
|
+
self.call.cancel()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ModalLab(HttpLab):
|
|
37
|
+
"""A lab sandbox: encrypted port + connect token, no outbound network, leased."""
|
|
38
|
+
|
|
39
|
+
def __init__(
|
|
40
|
+
self, sandbox: Any, url: str, connect_token: str, lab_token: str, leases: Any
|
|
41
|
+
) -> None:
|
|
42
|
+
super().__init__(url, connect_token, lab_token=lab_token, on_stop=self._terminate)
|
|
43
|
+
self.sandbox = sandbox
|
|
44
|
+
self.leases = leases
|
|
45
|
+
|
|
46
|
+
def heartbeat(self, lease_s: float) -> dict[str, Any]:
|
|
47
|
+
data = super().heartbeat(lease_s)
|
|
48
|
+
self.leases[self.sandbox.object_id] = time.time() + lease_s
|
|
49
|
+
return data
|
|
50
|
+
|
|
51
|
+
def _terminate(self) -> None:
|
|
52
|
+
self.leases.pop(self.sandbox.object_id, None)
|
|
53
|
+
self.sandbox.terminate()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class ModalBackend:
|
|
57
|
+
def __init__(self, *, store_volume: Any) -> None:
|
|
58
|
+
import modal
|
|
59
|
+
|
|
60
|
+
self.modal = modal
|
|
61
|
+
self.store_volume = store_volume
|
|
62
|
+
self.leases = modal.Dict.from_name(
|
|
63
|
+
LEASE_DICT, environment_name=ENVIRONMENT, create_if_missing=True
|
|
64
|
+
)
|
|
65
|
+
self.labs: list[ModalLab] = []
|
|
66
|
+
|
|
67
|
+
def _fn(self, name: str) -> Any:
|
|
68
|
+
return self.modal.Function.from_name(APP_NAME, name, environment_name=ENVIRONMENT)
|
|
69
|
+
|
|
70
|
+
def start_lab(self, record: SessionRecord) -> ModalLab:
|
|
71
|
+
from wspx.cloud.app import app, lab_image, models_volume
|
|
72
|
+
|
|
73
|
+
path = self._fn("download").remote(record.model, record.revision)
|
|
74
|
+
self.store_volume.commit() # the lab mounts the store read-only: give it the latest state
|
|
75
|
+
lab_token = secrets.token_urlsafe(24)
|
|
76
|
+
args = [
|
|
77
|
+
"python",
|
|
78
|
+
"-m",
|
|
79
|
+
"wspx.lab.server",
|
|
80
|
+
"--model",
|
|
81
|
+
path,
|
|
82
|
+
"--session",
|
|
83
|
+
record.id,
|
|
84
|
+
"--store",
|
|
85
|
+
"/store",
|
|
86
|
+
"--port",
|
|
87
|
+
str(LAB_PORT),
|
|
88
|
+
"--dtype",
|
|
89
|
+
record.dtype,
|
|
90
|
+
]
|
|
91
|
+
if record.branch:
|
|
92
|
+
args += ["--branch", record.branch]
|
|
93
|
+
if record.snapshot:
|
|
94
|
+
args += ["--snapshot", record.snapshot]
|
|
95
|
+
if record.trust_remote_code:
|
|
96
|
+
args.append("--trust-remote-code")
|
|
97
|
+
sandbox = self.modal.Sandbox.create(
|
|
98
|
+
*args,
|
|
99
|
+
app=app,
|
|
100
|
+
image=lab_image,
|
|
101
|
+
gpu=record.gpu,
|
|
102
|
+
timeout=SANDBOX_MAX_S,
|
|
103
|
+
idle_timeout=int(record.grace_s) + 600,
|
|
104
|
+
volumes={"/models": models_volume.read_only(), "/store": self.store_volume.read_only()},
|
|
105
|
+
outbound_cidr_allowlist=[], # an empty allowlist: no outbound network at all
|
|
106
|
+
outbound_domain_allowlist=[],
|
|
107
|
+
encrypted_ports=[LAB_PORT],
|
|
108
|
+
env={"WSPX_LAB_TOKEN": lab_token, "HF_HUB_OFFLINE": "1"},
|
|
109
|
+
environment_name=ENVIRONMENT,
|
|
110
|
+
)
|
|
111
|
+
sandbox.set_tags({TAG_KEY: TAG_VALUE, "session": record.id})
|
|
112
|
+
self.leases[sandbox.object_id] = time.time() + 600
|
|
113
|
+
creds = sandbox.create_connect_token(user_metadata={"session": record.id}, port=LAB_PORT)
|
|
114
|
+
lab = ModalLab(sandbox, creds.url, creds.token, lab_token, self.leases)
|
|
115
|
+
deadline = time.time() + READY_TIMEOUT_S
|
|
116
|
+
while time.time() < deadline:
|
|
117
|
+
try:
|
|
118
|
+
if lab.client.get("/health", timeout=10).status_code == 200:
|
|
119
|
+
break
|
|
120
|
+
except httpx.HTTPError:
|
|
121
|
+
pass
|
|
122
|
+
if sandbox.poll() is not None:
|
|
123
|
+
raise RuntimeError(f"lab exited while loading: {sandbox.stderr.read()[-2000:]}")
|
|
124
|
+
time.sleep(3)
|
|
125
|
+
else:
|
|
126
|
+
sandbox.terminate()
|
|
127
|
+
raise RuntimeError("lab did not become ready in time")
|
|
128
|
+
self.labs.append(lab)
|
|
129
|
+
return lab
|
|
130
|
+
|
|
131
|
+
def start_runner(self, record: SessionRecord, env: dict[str, str]) -> ModalRunner:
|
|
132
|
+
del record
|
|
133
|
+
return ModalRunner(self._fn("runner").spawn(env))
|
|
134
|
+
|
|
135
|
+
def stop_all(self) -> int:
|
|
136
|
+
n = 0
|
|
137
|
+
for lab in self.labs:
|
|
138
|
+
lab.stop()
|
|
139
|
+
n += 1
|
|
140
|
+
self.labs.clear()
|
|
141
|
+
return n
|
wspx/cloud/compose.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""``wspx deploy --docker``: the same services under Docker Compose on one GPU box.
|
|
2
|
+
|
|
3
|
+
The control plane runs labs in-process here, so a lab shares the control
|
|
4
|
+
plane's container. Keys still live only in the gateway container, which is
|
|
5
|
+
the only one with them in its environment, but untrusted model code and agent
|
|
6
|
+
scripts are not isolated the way a Modal sandbox isolates them. Use this for
|
|
7
|
+
models and scripts you trust.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from wspx import __version__
|
|
15
|
+
|
|
16
|
+
COMPOSE = """\
|
|
17
|
+
# wspx {version}: self-hosted. Fill in .env, then `docker compose up -d`.
|
|
18
|
+
services:
|
|
19
|
+
gateway:
|
|
20
|
+
image: python:3.12-slim
|
|
21
|
+
command: >-
|
|
22
|
+
sh -c "pip install --quiet 'wspx[gateway]=={version}' &&
|
|
23
|
+
uvicorn --factory wspx.gateway.app:create_app_from_env --host 0.0.0.0 --port 8000"
|
|
24
|
+
environment:
|
|
25
|
+
WSPX_GATEWAY_SECRET: ${{WSPX_GATEWAY_SECRET}}
|
|
26
|
+
WSPX_ADMIN_TOKEN: ${{WSPX_ADMIN_TOKEN}}
|
|
27
|
+
WSPX_LEDGER: /ledger/ledger.db
|
|
28
|
+
ANTHROPIC_API_KEY: ${{ANTHROPIC_API_KEY:-}}
|
|
29
|
+
OPENAI_API_KEY: ${{OPENAI_API_KEY:-}}
|
|
30
|
+
GEMINI_API_KEY: ${{GEMINI_API_KEY:-}}
|
|
31
|
+
volumes: [ledger:/ledger]
|
|
32
|
+
networks: [internal]
|
|
33
|
+
|
|
34
|
+
control:
|
|
35
|
+
image: pytorch/pytorch:latest
|
|
36
|
+
command: >-
|
|
37
|
+
sh -c "pip install --quiet 'wspx[control,lab,driver]=={version}' &&
|
|
38
|
+
uvicorn --factory wspx.control.app:create_app_from_env --host 0.0.0.0 --port 8765"
|
|
39
|
+
environment:
|
|
40
|
+
WSPX_STORE: /store
|
|
41
|
+
WSPX_API_TOKEN: ${{WSPX_API_TOKEN}}
|
|
42
|
+
WSPX_GATEWAY_SECRET: ${{WSPX_GATEWAY_SECRET}}
|
|
43
|
+
WSPX_ADMIN_TOKEN: ${{WSPX_ADMIN_TOKEN}}
|
|
44
|
+
WSPX_GATEWAY_URL: http://gateway:8000
|
|
45
|
+
WSPX_PUBLIC_URL: http://localhost:8765
|
|
46
|
+
HF_TOKEN: ${{HF_TOKEN:-}}
|
|
47
|
+
ports: ["127.0.0.1:8765:8765"]
|
|
48
|
+
volumes: [store:/store, models:/root/.cache/huggingface]
|
|
49
|
+
networks: [internal, default]
|
|
50
|
+
deploy:
|
|
51
|
+
resources:
|
|
52
|
+
reservations:
|
|
53
|
+
devices: [{{driver: nvidia, count: all, capabilities: [gpu]}}]
|
|
54
|
+
|
|
55
|
+
volumes: {{ledger: {{}}, store: {{}}, models: {{}}}}
|
|
56
|
+
networks: {{internal: {{internal: true}}}}
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
ENV = """\
|
|
60
|
+
WSPX_API_TOKEN={api}
|
|
61
|
+
WSPX_GATEWAY_SECRET={secret}
|
|
62
|
+
WSPX_ADMIN_TOKEN={admin}
|
|
63
|
+
ANTHROPIC_API_KEY=
|
|
64
|
+
OPENAI_API_KEY=
|
|
65
|
+
GEMINI_API_KEY=
|
|
66
|
+
HF_TOKEN=
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def write_compose(directory: Path) -> Path:
|
|
71
|
+
import secrets
|
|
72
|
+
|
|
73
|
+
path = directory / "docker-compose.yml"
|
|
74
|
+
path.write_text(COMPOSE.format(version=__version__))
|
|
75
|
+
env = directory / ".env"
|
|
76
|
+
if not env.exists():
|
|
77
|
+
env.write_text(
|
|
78
|
+
ENV.format(
|
|
79
|
+
api="wspx_" + secrets.token_urlsafe(18),
|
|
80
|
+
secret=secrets.token_urlsafe(32),
|
|
81
|
+
admin=secrets.token_urlsafe(32),
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
env.chmod(0o600)
|
|
85
|
+
return path
|
wspx/cloud/deploy.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""``wspx deploy``: put the app into the user's Modal account."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import secrets
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
|
|
11
|
+
from wspx.auth import new_secret
|
|
12
|
+
from wspx.cloud import APP_NAME, CONTROL_SECRET, ENVIRONMENT, KEY_SECRETS
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class Deployment:
|
|
17
|
+
control_url: str
|
|
18
|
+
gateway_url: str
|
|
19
|
+
api_token: str
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _modal(*args: str, check: bool = True) -> subprocess.CompletedProcess[str]:
|
|
23
|
+
return subprocess.run(
|
|
24
|
+
[sys.executable, "-m", "modal", *args], capture_output=True, text=True, check=check
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def ensure_environment() -> None:
|
|
29
|
+
proc = _modal("environment", "list", "--json", check=False)
|
|
30
|
+
if ENVIRONMENT not in proc.stdout:
|
|
31
|
+
_modal("environment", "create", ENVIRONMENT, check=False)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def ensure_secrets(api_token: str | None = None) -> str:
|
|
35
|
+
"""Create the control secret once (API token, shared secret, admin token) and an empty keys secret."""
|
|
36
|
+
proc = _modal("secret", "list", "--env", ENVIRONMENT, "--json", check=False)
|
|
37
|
+
api_token = api_token or "wspx_" + secrets.token_urlsafe(24)
|
|
38
|
+
if CONTROL_SECRET not in proc.stdout:
|
|
39
|
+
_modal(
|
|
40
|
+
"secret",
|
|
41
|
+
"create",
|
|
42
|
+
CONTROL_SECRET,
|
|
43
|
+
f"WSPX_API_TOKEN={api_token}",
|
|
44
|
+
f"WSPX_GATEWAY_SECRET={new_secret()}",
|
|
45
|
+
f"WSPX_ADMIN_TOKEN={new_secret()}",
|
|
46
|
+
"--env",
|
|
47
|
+
ENVIRONMENT,
|
|
48
|
+
)
|
|
49
|
+
else:
|
|
50
|
+
api_token = "" # already created; the CLI keeps its own copy
|
|
51
|
+
for name, _ in KEY_SECRETS.values():
|
|
52
|
+
if name not in proc.stdout: # every key secret must exist for the app to deploy
|
|
53
|
+
_modal("secret", "create", name, "WSPX_UNSET=1", "--env", ENVIRONMENT)
|
|
54
|
+
return api_token
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def set_key(provider: str, value: str) -> None:
|
|
58
|
+
"""Store a provider key as that provider's secret, overwriting it in place.
|
|
59
|
+
|
|
60
|
+
The value goes to Modal over its API, so it never lands on local disk or in
|
|
61
|
+
a process list (``modal secret create KEY=value`` would put it in argv).
|
|
62
|
+
Works before the first deploy: the environment is created if needed.
|
|
63
|
+
"""
|
|
64
|
+
if provider not in KEY_SECRETS:
|
|
65
|
+
raise ValueError(f"unknown provider {provider!r}; use one of {', '.join(KEY_SECRETS)}")
|
|
66
|
+
value = value.strip()
|
|
67
|
+
if not value:
|
|
68
|
+
raise ValueError("the key is empty")
|
|
69
|
+
ensure_environment()
|
|
70
|
+
name, var = KEY_SECRETS[provider]
|
|
71
|
+
_write_secret(name, {var: value})
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _write_secret(name: str, values: dict[str, str]) -> None:
|
|
75
|
+
"""Create or overwrite a secret in place, over the API (values never reach argv)."""
|
|
76
|
+
import modal
|
|
77
|
+
|
|
78
|
+
# The overwrite path `modal secret create --force` uses; fall back to the CLI if it moves.
|
|
79
|
+
create_deployed = getattr(modal.Secret, "_create_deployed", None)
|
|
80
|
+
if create_deployed is None:
|
|
81
|
+
pairs = [f"{k}={v}" for k, v in values.items()]
|
|
82
|
+
_modal("secret", "create", name, *pairs, "--env", ENVIRONMENT, "--force")
|
|
83
|
+
return
|
|
84
|
+
create_deployed(name, values, environment_name=ENVIRONMENT, overwrite=True)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def remove_key(provider: str) -> None:
|
|
88
|
+
"""Empty a provider's secret (it must keep existing: deployed functions reference it)."""
|
|
89
|
+
if provider not in KEY_SECRETS:
|
|
90
|
+
raise ValueError(f"unknown provider {provider!r}; use one of {', '.join(KEY_SECRETS)}")
|
|
91
|
+
_write_secret(KEY_SECRETS[provider][0], {"WSPX_UNSET": "1"})
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def deploy() -> Deployment: # pragma: no cover - needs a Modal account
|
|
95
|
+
ensure_environment()
|
|
96
|
+
token = ensure_secrets()
|
|
97
|
+
os.environ.setdefault("MODAL_ENVIRONMENT", ENVIRONMENT)
|
|
98
|
+
from wspx.cloud.app import app, control, gateway
|
|
99
|
+
|
|
100
|
+
with __import__("modal").enable_output():
|
|
101
|
+
app.deploy(name=APP_NAME, environment_name=ENVIRONMENT)
|
|
102
|
+
return Deployment(control.get_web_url() or "", gateway.get_web_url() or "", token)
|