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.
Files changed (99) hide show
  1. wspx/__init__.py +24 -0
  2. wspx/_version.py +1 -0
  3. wspx/addressing.py +415 -0
  4. wspx/auth.py +77 -0
  5. wspx/cli/__init__.py +1 -0
  6. wspx/cli/client.py +124 -0
  7. wspx/cli/config.py +89 -0
  8. wspx/cli/connect.py +88 -0
  9. wspx/cli/main.py +848 -0
  10. wspx/cloud/__init__.py +20 -0
  11. wspx/cloud/app.py +191 -0
  12. wspx/cloud/backend.py +141 -0
  13. wspx/cloud/compose.py +85 -0
  14. wspx/cloud/deploy.py +102 -0
  15. wspx/config.py +189 -0
  16. wspx/control/__init__.py +5 -0
  17. wspx/control/app.py +488 -0
  18. wspx/control/backend.py +244 -0
  19. wspx/control/gateway_client.py +79 -0
  20. wspx/control/mcp_server.py +71 -0
  21. wspx/control/sessions.py +424 -0
  22. wspx/control/tracedb.py +183 -0
  23. wspx/control/ui/assets/index-BJFweZAn.css +1 -0
  24. wspx/control/ui/assets/index-DhUVhWIK.js +10 -0
  25. wspx/control/ui/index.html +13 -0
  26. wspx/control/views.py +180 -0
  27. wspx/diagnostics.py +146 -0
  28. wspx/driver/__init__.py +7 -0
  29. wspx/driver/loop.py +255 -0
  30. wspx/driver/messages.py +62 -0
  31. wspx/driver/providers/__init__.py +84 -0
  32. wspx/driver/providers/anthropic.py +149 -0
  33. wspx/driver/providers/compat.py +115 -0
  34. wspx/driver/providers/google.py +138 -0
  35. wspx/driver/providers/openai.py +125 -0
  36. wspx/driver/run.py +134 -0
  37. wspx/errors.py +69 -0
  38. wspx/gateway/__init__.py +8 -0
  39. wspx/gateway/app.py +626 -0
  40. wspx/gateway/ledger.py +536 -0
  41. wspx/gateway/prices.py +75 -0
  42. wspx/gateway/usage.py +284 -0
  43. wspx/handles.py +117 -0
  44. wspx/integrations/AGENTS.snippet.md +8 -0
  45. wspx/integrations/SKILL.md +52 -0
  46. wspx/integrations/__init__.py +17 -0
  47. wspx/integrations/claude-plugin/.claude-plugin/plugin.json +30 -0
  48. wspx/integrations/claude-plugin/skills/wspx/SKILL.md +52 -0
  49. wspx/integrations/codex-hooks.toml +9 -0
  50. wspx/lab/__init__.py +5 -0
  51. wspx/lab/analysis.py +485 -0
  52. wspx/lab/check.py +489 -0
  53. wspx/lab/evaluate.py +108 -0
  54. wspx/lab/export.py +275 -0
  55. wspx/lab/grep.py +754 -0
  56. wspx/lab/index.py +239 -0
  57. wspx/lab/ir/__init__.py +1 -0
  58. wspx/lab/ir/capture.py +299 -0
  59. wspx/lab/ir/graph.py +148 -0
  60. wspx/lab/ir/model_ir.py +522 -0
  61. wspx/lab/ir/semantics.py +1255 -0
  62. wspx/lab/ir/tape.py +853 -0
  63. wspx/lab/loader.py +204 -0
  64. wspx/lab/merge.py +200 -0
  65. wspx/lab/ops/__init__.py +76 -0
  66. wspx/lab/ops/builtin.py +379 -0
  67. wspx/lab/patch.py +320 -0
  68. wspx/lab/probe.py +206 -0
  69. wspx/lab/results.py +114 -0
  70. wspx/lab/sdk.py +120 -0
  71. wspx/lab/server.py +236 -0
  72. wspx/lab/session.py +1574 -0
  73. wspx/lab/tensors.py +119 -0
  74. wspx/lab/text.py +69 -0
  75. wspx/lab/tree.py +302 -0
  76. wspx/loops.py +127 -0
  77. wspx/project.py +143 -0
  78. wspx/py.typed +0 -0
  79. wspx/safety/__init__.py +15 -0
  80. wspx/safety/kill.py +47 -0
  81. wspx/safety/sweeper.py +68 -0
  82. wspx/store/__init__.py +64 -0
  83. wspx/store/objects.py +130 -0
  84. wspx/store/overlay.py +209 -0
  85. wspx/store/refs.py +150 -0
  86. wspx/testing/__init__.py +21 -0
  87. wspx/testing/api.py +155 -0
  88. wspx/testing/builtin.py +139 -0
  89. wspx/testing/runner.py +257 -0
  90. wspx/tools/__init__.py +17 -0
  91. wspx/tools/dispatch.py +129 -0
  92. wspx/tools/schemas.py +313 -0
  93. wspx/vcs/__init__.py +17 -0
  94. wspx/vcs/repo.py +431 -0
  95. wspx-0.1.0.dev0.dist-info/METADATA +278 -0
  96. wspx-0.1.0.dev0.dist-info/RECORD +99 -0
  97. wspx-0.1.0.dev0.dist-info/WHEEL +4 -0
  98. wspx-0.1.0.dev0.dist-info/entry_points.txt +10 -0
  99. wspx-0.1.0.dev0.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,244 @@
1
+ """Where labs and runners run: in-process (local) or on Modal (cloud)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import tempfile
6
+ import threading
7
+ from collections.abc import Callable
8
+ from dataclasses import dataclass, field
9
+ from pathlib import Path
10
+ from typing import Any, Protocol
11
+
12
+ import httpx
13
+
14
+
15
+ @dataclass
16
+ class SessionRecord:
17
+ id: str
18
+ model: str
19
+ driver: str | None = None
20
+ task: str = ""
21
+ gpu: str | None = None
22
+ branch: str | None = None
23
+ revision: str | None = None
24
+ dtype: str = "bfloat16"
25
+ trust_remote_code: bool = False
26
+ status: str = "idle"
27
+ """idle · running · paused · stopped · done · error"""
28
+ tier: str = "cold"
29
+ """hot · grace · cold"""
30
+ grace_s: float = 900.0
31
+ keep_warm: str = "auto"
32
+ project: str = ""
33
+ snapshot: str | None = None
34
+ """Object id of the project snapshot the lab unpacks as its working directory."""
35
+ created: float = 0.0
36
+ fingerprint: str | None = None
37
+ reason: str = ""
38
+ load_seconds: float | None = None
39
+ extra: dict[str, Any] = field(default_factory=dict)
40
+
41
+ def to_dict(self) -> dict[str, Any]:
42
+ return dict(self.__dict__)
43
+
44
+
45
+ class LabHandle(Protocol):
46
+ def call(
47
+ self, tool: str, args: dict[str, Any], call_id: str, context: dict[str, Any]
48
+ ) -> dict[str, Any]: ...
49
+
50
+ def heartbeat(self, lease_s: float) -> dict[str, Any]: ...
51
+
52
+ def stop(self) -> None: ...
53
+
54
+
55
+ class RunnerHandle(Protocol):
56
+ def alive(self) -> bool: ...
57
+
58
+ def stop(self) -> None: ...
59
+
60
+
61
+ class Backend(Protocol):
62
+ def start_lab(self, record: SessionRecord) -> LabHandle: ...
63
+
64
+ def start_runner(self, record: SessionRecord, env: dict[str, str]) -> RunnerHandle: ...
65
+
66
+ def stop_all(self) -> int: ...
67
+
68
+
69
+ class HttpLab:
70
+ """A lab reached over HTTP (a sandbox's encrypted port and connect token)."""
71
+
72
+ def __init__(
73
+ self,
74
+ url: str,
75
+ token: str | None,
76
+ *,
77
+ lab_token: str | None = None,
78
+ on_stop: Callable[[], None] | None = None,
79
+ timeout: float = 3600.0,
80
+ ) -> None:
81
+ headers = {"authorization": f"Bearer {token}"} if token else {}
82
+ if lab_token:
83
+ headers["x-wspx-lab-token"] = lab_token
84
+ self.client = httpx.Client(base_url=url.rstrip("/"), headers=headers, timeout=timeout)
85
+ self.on_stop = on_stop
86
+
87
+ def call(
88
+ self, tool: str, args: dict[str, Any], call_id: str, context: dict[str, Any]
89
+ ) -> dict[str, Any]:
90
+ r = self.client.post(
91
+ f"/tool/{tool}", json={"args": args, "call_id": call_id, "context": context}
92
+ )
93
+ r.raise_for_status()
94
+ data: dict[str, Any] = r.json()
95
+ return data
96
+
97
+ def heartbeat(self, lease_s: float) -> dict[str, Any]:
98
+ r = self.client.post("/heartbeat", json={"lease_s": lease_s}, timeout=30)
99
+ r.raise_for_status()
100
+ data: dict[str, Any] = r.json()
101
+ return data
102
+
103
+ def stop(self) -> None:
104
+ if self.on_stop is not None:
105
+ self.on_stop()
106
+ self.client.close()
107
+
108
+
109
+ class InProcessLab:
110
+ """A lab in this process, driven through the same server code and journal as the cloud."""
111
+
112
+ def __init__(self, server: Any) -> None:
113
+ from starlette.testclient import TestClient
114
+
115
+ self.server = server
116
+ self.client = TestClient(server.app())
117
+
118
+ def call(
119
+ self, tool: str, args: dict[str, Any], call_id: str, context: dict[str, Any]
120
+ ) -> dict[str, Any]:
121
+ r = self.client.post(
122
+ f"/tool/{tool}", json={"args": args, "call_id": call_id, "context": context}
123
+ )
124
+ r.raise_for_status()
125
+ data: dict[str, Any] = r.json()
126
+ return data
127
+
128
+ def heartbeat(self, lease_s: float) -> dict[str, Any]:
129
+ data: dict[str, Any] = self.client.post("/heartbeat", json={"lease_s": lease_s}).json()
130
+ return data
131
+
132
+ def stop(self) -> None:
133
+ self.client.close()
134
+
135
+
136
+ class ThreadRunner:
137
+ def __init__(self, target: Callable[[], Any]) -> None:
138
+ self._stop = threading.Event()
139
+ self.thread = threading.Thread(target=target, name="wspx-runner", daemon=True)
140
+ self.thread.start()
141
+
142
+ def alive(self) -> bool:
143
+ return self.thread.is_alive()
144
+
145
+ def stop(self) -> None:
146
+ self._stop.set()
147
+
148
+
149
+ class LocalBackend:
150
+ """Labs and runners in this process: for development, tests and single-box self-hosting.
151
+
152
+ ``lab_factory`` builds the Lab for a record (default: load the model with
153
+ the HF loader). The lab still sees a read-only view of the store through an
154
+ overlay, so the journal path is exercised exactly as in the cloud.
155
+ """
156
+
157
+ def __init__(
158
+ self,
159
+ store_root: str | Path,
160
+ *,
161
+ lab_factory: Callable[[SessionRecord, Any], Any] | None = None,
162
+ runner_factory: Callable[[SessionRecord, dict[str, str]], Callable[[], Any]] | None = None,
163
+ ) -> None:
164
+ self.store_root = Path(store_root)
165
+ self.lab_factory = lab_factory
166
+ self.runner_factory = runner_factory
167
+ self.labs: list[InProcessLab] = []
168
+ self.runners: list[ThreadRunner] = []
169
+
170
+ def start_lab(self, record: SessionRecord) -> LabHandle:
171
+ from wspx.lab.server import LabServer
172
+ from wspx.store.overlay import OverlayStore
173
+
174
+ overlay = OverlayStore(self.store_root, tempfile.mkdtemp(prefix=f"wspx-{record.id}-"))
175
+ lab = (
176
+ self.lab_factory(record, overlay)
177
+ if self.lab_factory is not None
178
+ else _load_lab(record, overlay)
179
+ )
180
+ handle = InProcessLab(LabServer(lab, exit_on_lapse=False))
181
+ self.labs.append(handle)
182
+ return handle
183
+
184
+ def start_runner(self, record: SessionRecord, env: dict[str, str]) -> RunnerHandle:
185
+ if self.runner_factory is None:
186
+ from wspx.driver.run import run_from_env
187
+
188
+ def target() -> Any:
189
+ return run_from_env(env)
190
+ else:
191
+ target = self.runner_factory(record, env)
192
+ runner = ThreadRunner(target)
193
+ self.runners.append(runner)
194
+ return runner
195
+
196
+ def stop_all(self) -> int:
197
+ for lab in self.labs:
198
+ lab.stop()
199
+ n = len(self.labs)
200
+ self.labs.clear()
201
+ return n
202
+
203
+
204
+ def _load_lab(record: SessionRecord, store: Any) -> Any:
205
+ from wspx.config import WspxConfig
206
+ from wspx.lab.ir.capture import build_ir
207
+ from wspx.lab.loader import load_hf
208
+ from wspx.lab.sdk import _cached_ir, _save_ir
209
+ from wspx.lab.session import Lab
210
+
211
+ workdir = project_workdir(record, store)
212
+ loaded = load_hf(
213
+ record.model,
214
+ revision=record.revision,
215
+ dtype=record.dtype,
216
+ trust_remote_code=record.trust_remote_code,
217
+ )
218
+ ir = _cached_ir(store, loaded.fingerprint)
219
+ if ir is None:
220
+ ir = build_ir(loaded.model, fingerprint=loaded.fingerprint, hash_params=False)
221
+ _save_ir(store, ir)
222
+ record.fingerprint = loaded.fingerprint
223
+ return Lab(
224
+ loaded.model,
225
+ ir,
226
+ store,
227
+ session=record.id,
228
+ branch=record.branch,
229
+ tokenizer=loaded.tokenizer,
230
+ base_reader=loaded.base_reader if loaded.readable else None,
231
+ workdir=workdir,
232
+ config=WspxConfig.discover(workdir) if workdir else None,
233
+ )
234
+
235
+
236
+ def project_workdir(record: SessionRecord, store: Any) -> Path | None:
237
+ """Unpack the session's project snapshot into a fresh working directory, if it has one."""
238
+ if not record.snapshot:
239
+ return None
240
+ from wspx.project import extract
241
+
242
+ return extract(
243
+ store.objects.get(record.snapshot), tempfile.mkdtemp(prefix=f"wspx-{record.id}-work-")
244
+ )
@@ -0,0 +1,79 @@
1
+ """The control plane's view of the gateway's admin API."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any
6
+
7
+ import httpx
8
+
9
+ from wspx.errors import WspxError
10
+
11
+
12
+ class GatewayAdmin:
13
+ def __init__(
14
+ self, url: str, token: str, *, transport: httpx.BaseTransport | None = None
15
+ ) -> None:
16
+ self.client = httpx.Client(
17
+ base_url=url.rstrip("/"),
18
+ headers={"authorization": f"Bearer {token}"},
19
+ timeout=30.0,
20
+ transport=transport,
21
+ )
22
+
23
+ def _req(self, method: str, path: str, **kw: Any) -> Any:
24
+ try:
25
+ r = self.client.request(method, path, **kw)
26
+ except httpx.HTTPError as exc:
27
+ raise WspxError(f"gateway unreachable: {exc}") from exc
28
+ if r.status_code >= 400:
29
+ raise WspxError(f"gateway {path}: {r.text[:300]}")
30
+ return r.json()
31
+
32
+ def keys(self) -> dict[str, Any]:
33
+ data: dict[str, Any] = self._req("GET", "/admin/keys")["keys"]
34
+ return data
35
+
36
+ def recycle(self) -> bool:
37
+ return bool(self._req("POST", "/admin/recycle")["recycled"])
38
+
39
+ def limits(self) -> list[dict[str, Any]]:
40
+ data: list[dict[str, Any]] = self._req("GET", "/admin/limits")["limits"]
41
+ return data
42
+
43
+ def set_limit(self, limit: dict[str, Any]) -> dict[str, Any]:
44
+ data: dict[str, Any] = self._req("POST", "/admin/limits", json=limit)
45
+ return data
46
+
47
+ def remove_limit(self, limit_id: str) -> bool:
48
+ return bool(self._req("DELETE", "/admin/limits", params={"id": limit_id})["removed"])
49
+
50
+ def usage(self, session: str | None = None, since: float = 0.0) -> dict[str, Any]:
51
+ params: dict[str, Any] = {"since": since}
52
+ if session:
53
+ params["session"] = session
54
+ data: dict[str, Any] = self._req("GET", "/admin/usage", params=params)
55
+ return data
56
+
57
+ def halt(self, reason: str = "kill --all") -> None:
58
+ self._req("POST", "/admin/halt", json={"reason": reason})
59
+
60
+ def resume(self, session: str | None = None) -> None:
61
+ self._req("POST", "/admin/resume", json={"session": session} if session else {})
62
+
63
+ def pause(self, session: str, reason: str) -> None:
64
+ self._req("POST", f"/admin/sessions/{session}/pause", json={"reason": reason})
65
+
66
+ def session(self, session: str) -> dict[str, Any]:
67
+ data: dict[str, Any] = self._req("GET", f"/admin/sessions/{session}")
68
+ return data
69
+
70
+ def price(self, model: str, inp: float, out: float) -> None:
71
+ self._req("POST", "/admin/prices", json={"model": model, "input": inp, "output": out})
72
+
73
+ def observe(self, session: str, kind: str, key: Any, label: str = "") -> dict[str, Any] | None:
74
+ verdict: dict[str, Any] | None = self._req(
75
+ "POST",
76
+ "/admin/observe",
77
+ json={"session": session, "kind": kind, "key": key, "label": label},
78
+ )["verdict"]
79
+ return verdict
@@ -0,0 +1,71 @@
1
+ """The hosted MCP endpoint (streamable HTTP) and the local stdio proxy (§5.10).
2
+
3
+ Both expose the same tools as the built-in driver. A client binds to a session
4
+ with the ``X-Wspx-Session`` header (or ``?session=``); every call is logged
5
+ in the trace DB under that session with a ``call_id`` the client's hooks can
6
+ join on.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from collections.abc import Callable
12
+ from typing import Any
13
+
14
+ import anyio
15
+ from mcp import types
16
+ from mcp.server.lowlevel import Server
17
+
18
+ from wspx import __version__
19
+ from wspx.integrations import skill_text
20
+ from wspx.tools import mcp_tools
21
+
22
+ CallFn = Callable[[str | None, str, dict[str, Any]], dict[str, Any]]
23
+ """(session or None, tool, arguments) → tool result dict."""
24
+
25
+
26
+ def _tool_list() -> list[types.Tool]:
27
+ return [
28
+ types.Tool(name=t["name"], description=t["description"], input_schema=t["inputSchema"])
29
+ for t in mcp_tools()
30
+ ]
31
+
32
+
33
+ def build_server(call: CallFn) -> Server[Any]:
34
+ async def list_tools(_ctx: Any, _params: Any) -> types.ListToolsResult:
35
+ return types.ListToolsResult(tools=_tool_list())
36
+
37
+ async def call_tool(ctx: Any, params: types.CallToolRequestParams) -> types.CallToolResult:
38
+ request = getattr(ctx, "request", None)
39
+ session = None
40
+ if request is not None:
41
+ session = request.headers.get("x-wspx-session") or request.query_params.get("session")
42
+ result = await anyio.to_thread.run_sync(
43
+ call, session, params.name, dict(params.arguments or {})
44
+ )
45
+ text = result.get("summary", "")
46
+ if result.get("call_id"):
47
+ text += f"\n[call {result['call_id']}]"
48
+ return types.CallToolResult(
49
+ content=[types.TextContent(type="text", text=text)], is_error=not result.get("ok", True)
50
+ )
51
+
52
+ return Server(
53
+ "wspx",
54
+ version=__version__,
55
+ instructions=skill_text(),
56
+ on_list_tools=list_tools,
57
+ on_call_tool=call_tool,
58
+ )
59
+
60
+
61
+ def run_stdio(call: CallFn) -> None:
62
+ """``wspx mcp``: a local stdio server forwarding to the hosted control plane."""
63
+ from mcp.server.stdio import stdio_server
64
+
65
+ server = build_server(call)
66
+
67
+ async def main() -> None:
68
+ async with stdio_server() as (read, write):
69
+ await server.run(read, write, server.create_initialization_options())
70
+
71
+ anyio.run(main)