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
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>wspx</title>
|
|
7
|
+
<script type="module" crossorigin src="/assets/index-DhUVhWIK.js"></script>
|
|
8
|
+
<link rel="stylesheet" crossorigin href="/assets/index-BJFweZAn.css">
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<div id="root"></div>
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
wspx/control/views.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""Read-only views for the UI, computed server-side from the store (§5.11).
|
|
2
|
+
|
|
3
|
+
The browser gets small JSON aggregates. Nothing here needs the lab, so every
|
|
4
|
+
view works while the lab is cold. Held-out per-item results appear here, and
|
|
5
|
+
only here: these endpoints take the user token, never a runner token.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from wspx.errors import StoreError
|
|
14
|
+
from wspx.store import Store, short
|
|
15
|
+
from wspx.vcs import Repo, normalize_tree, tree_key
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _ir(store: Store, fingerprint: str | None) -> dict[str, Any] | None:
|
|
19
|
+
if not fingerprint:
|
|
20
|
+
return None
|
|
21
|
+
raw = store.read_file(f"models/{fingerprint.replace(':', '_')}/ir.json")
|
|
22
|
+
return json.loads(raw) if raw else None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def architecture(store: Store, fingerprint: str | None) -> dict[str, Any]:
|
|
26
|
+
ir = _ir(store, fingerprint)
|
|
27
|
+
if ir is None:
|
|
28
|
+
return {"available": False}
|
|
29
|
+
params = ir["params"]
|
|
30
|
+
sem = ir["semantics"]
|
|
31
|
+
blocks: dict[int, dict[str, Any]] = {
|
|
32
|
+
b["index"]: {
|
|
33
|
+
"index": b["index"],
|
|
34
|
+
"module": b["module"],
|
|
35
|
+
"branches": b["branches"],
|
|
36
|
+
"roles": [],
|
|
37
|
+
}
|
|
38
|
+
for b in sem["blocks"]
|
|
39
|
+
}
|
|
40
|
+
specials = []
|
|
41
|
+
for role in sem["roles"]:
|
|
42
|
+
entry = {
|
|
43
|
+
"address": role["address"],
|
|
44
|
+
"param": role["param"],
|
|
45
|
+
"kind": role["kind"],
|
|
46
|
+
"shape": params[role["param"]]["shape"],
|
|
47
|
+
"split": role.get("split"),
|
|
48
|
+
"experts": role.get("experts"),
|
|
49
|
+
}
|
|
50
|
+
if role.get("block") is not None and role["block"] in blocks:
|
|
51
|
+
blocks[role["block"]]["roles"].append(entry)
|
|
52
|
+
else:
|
|
53
|
+
specials.append(entry)
|
|
54
|
+
coverage = ir.get("coverage", {})
|
|
55
|
+
return {
|
|
56
|
+
"available": True,
|
|
57
|
+
"arch": ir["arch"],
|
|
58
|
+
"fingerprint": ir["fingerprint"],
|
|
59
|
+
"params": len(params),
|
|
60
|
+
"numel": sum(_numel(p["shape"]) for p in params.values()),
|
|
61
|
+
"blocks": [blocks[i] for i in sorted(blocks)],
|
|
62
|
+
"specials": specials,
|
|
63
|
+
"tied": sem.get("tied", False),
|
|
64
|
+
"embed": sem.get("embed"),
|
|
65
|
+
"unembed": sem.get("unembed"),
|
|
66
|
+
"unexercised": sorted(k for k, c in coverage.items() if not c.get("exercised")),
|
|
67
|
+
"unassigned": sem.get("unassigned", []),
|
|
68
|
+
"head_dims": sem.get("head_dims", {}),
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _numel(shape: list[int]) -> int:
|
|
73
|
+
n = 1
|
|
74
|
+
for s in shape:
|
|
75
|
+
n *= s
|
|
76
|
+
return n
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def commits(
|
|
80
|
+
store: Store, fingerprint: str | None, session: str, limit: int = 200
|
|
81
|
+
) -> dict[str, Any]:
|
|
82
|
+
repo = Repo(store, fingerprint or "")
|
|
83
|
+
branches = repo.branches()
|
|
84
|
+
head = store.refs.read(f"sessions/{session}/HEAD")
|
|
85
|
+
current = store.refs.symbolic_target(f"sessions/{session}/HEAD")
|
|
86
|
+
seen: dict[str, dict[str, Any]] = {}
|
|
87
|
+
for start in [head, *branches.values()]:
|
|
88
|
+
if not start:
|
|
89
|
+
continue
|
|
90
|
+
for commit in repo.walk(start):
|
|
91
|
+
if commit.oid in seen or len(seen) >= limit:
|
|
92
|
+
continue
|
|
93
|
+
patches = []
|
|
94
|
+
for pid in commit.patches:
|
|
95
|
+
try:
|
|
96
|
+
record = repo.get_patch(pid)
|
|
97
|
+
except StoreError:
|
|
98
|
+
continue
|
|
99
|
+
patches.append(
|
|
100
|
+
{
|
|
101
|
+
"handle": record.handle,
|
|
102
|
+
"op": record.op,
|
|
103
|
+
"tensors": record.tensors,
|
|
104
|
+
"precision": record.precision,
|
|
105
|
+
}
|
|
106
|
+
)
|
|
107
|
+
tree = repo.get_tree(commit.tree)
|
|
108
|
+
seen[commit.oid] = {
|
|
109
|
+
"oid": commit.oid,
|
|
110
|
+
"short": commit.short,
|
|
111
|
+
"parents": commit.parents,
|
|
112
|
+
"message": commit.message,
|
|
113
|
+
"time": commit.time,
|
|
114
|
+
"status": commit.status,
|
|
115
|
+
"author": commit.author,
|
|
116
|
+
"patches": patches,
|
|
117
|
+
"tree": tree_key(tree),
|
|
118
|
+
"tensors": len(normalize_tree(tree)["merged"]),
|
|
119
|
+
"forced": commit.forced,
|
|
120
|
+
"merge": commit.merge,
|
|
121
|
+
"checks": commit.checks,
|
|
122
|
+
}
|
|
123
|
+
ordered = sorted(seen.values(), key=lambda c: -c["time"])
|
|
124
|
+
return {
|
|
125
|
+
"head": head,
|
|
126
|
+
"branch": current.removeprefix("heads/") if current else None,
|
|
127
|
+
"branches": dict(branches.items()),
|
|
128
|
+
"commits": ordered,
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def runs(store: Store, fingerprint: str | None, session: str) -> dict[str, Any]:
|
|
133
|
+
"""Every cached eval run on a commit of this session: the commits × suites matrix."""
|
|
134
|
+
history = commits(store, fingerprint, session)
|
|
135
|
+
by_tree: dict[str, list[str]] = {tree_key(None): ["base"]}
|
|
136
|
+
for c in history["commits"]:
|
|
137
|
+
by_tree.setdefault(c["tree"], []).append(c["short"])
|
|
138
|
+
rows = []
|
|
139
|
+
for key, labels in by_tree.items():
|
|
140
|
+
for rel in store.glob(f"runs/{key}", "*/*.json"):
|
|
141
|
+
raw = store.read_file(rel)
|
|
142
|
+
if raw is None:
|
|
143
|
+
continue
|
|
144
|
+
run = json.loads(raw)
|
|
145
|
+
rows.append(
|
|
146
|
+
{
|
|
147
|
+
"tree": key,
|
|
148
|
+
"commits": labels,
|
|
149
|
+
"suite": run["suite"],
|
|
150
|
+
"split": run["split"],
|
|
151
|
+
"seed": run["seed"],
|
|
152
|
+
"n": run["n"],
|
|
153
|
+
"passed": run["passed"],
|
|
154
|
+
"pass_rate": run["passed"] / run["n"] if run["n"] else None,
|
|
155
|
+
"mean": run.get("mean_score"),
|
|
156
|
+
"checks": run.get("checks", []),
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
heldout = store.read_file(f"sessions/{session}/state.json")
|
|
160
|
+
count = json.loads(heldout).get("heldout_queries", 0) if heldout else 0
|
|
161
|
+
return {"runs": rows, "heldout_queries": count}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def handles(store: Store, session: str) -> dict[str, Any]:
|
|
165
|
+
raw = store.read_file(f"sessions/{session}/state.json")
|
|
166
|
+
if raw is None:
|
|
167
|
+
return {"handles": []}
|
|
168
|
+
state = json.loads(raw)
|
|
169
|
+
return {
|
|
170
|
+
"handles": state.get("handles", []),
|
|
171
|
+
"staged": [short(p, 8) for p in state.get("staged", [])],
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def json_object(store: Store, oid: str) -> Any:
|
|
176
|
+
data = store.objects.get(store.objects.resolve_prefix(oid))
|
|
177
|
+
try:
|
|
178
|
+
return json.loads(data)
|
|
179
|
+
except (UnicodeDecodeError, json.JSONDecodeError):
|
|
180
|
+
raise StoreError("that object is not JSON") from None
|
wspx/diagnostics.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""Compiler-style diagnostics (design §5.4).
|
|
2
|
+
|
|
3
|
+
Every diagnostic carries a machine-readable list of fixes: flagging a problem
|
|
4
|
+
without a way out sends agents round in loops.
|
|
5
|
+
|
|
6
|
+
Codes: ``E1xx`` block the edit; ``W1xx`` are static warnings, ``W2xx`` numeric
|
|
7
|
+
and ``W3xx`` smoke. Severity is separate from the code because some warnings
|
|
8
|
+
escalate with damage (``W203`` becomes an error past ``subulp_error_at``).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from enum import StrEnum
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Severity(StrEnum):
|
|
20
|
+
ERROR = "error"
|
|
21
|
+
WARNING = "warning"
|
|
22
|
+
INFO = "info"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Fix(BaseModel):
|
|
26
|
+
model_config = ConfigDict(frozen=True)
|
|
27
|
+
|
|
28
|
+
title: str
|
|
29
|
+
"""What to do, in a few words."""
|
|
30
|
+
action: dict[str, Any] = Field(default_factory=dict)
|
|
31
|
+
"""Machine-readable form, e.g. ``{"precision": "adapter"}`` or ``{"scale_at_least": 2}``."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Diagnostic(BaseModel):
|
|
35
|
+
model_config = ConfigDict(frozen=True)
|
|
36
|
+
|
|
37
|
+
code: str
|
|
38
|
+
severity: Severity
|
|
39
|
+
message: str
|
|
40
|
+
target: str | None = None
|
|
41
|
+
"""The address or tensor the diagnostic is about."""
|
|
42
|
+
detail: list[str] = Field(default_factory=list)
|
|
43
|
+
fixes: list[Fix] = Field(default_factory=list)
|
|
44
|
+
data: dict[str, Any] = Field(default_factory=dict)
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def is_error(self) -> bool:
|
|
48
|
+
return self.severity is Severity.ERROR
|
|
49
|
+
|
|
50
|
+
def render(self) -> str:
|
|
51
|
+
head = (
|
|
52
|
+
f"{self.code} {self.target}: {self.message}"
|
|
53
|
+
if self.target
|
|
54
|
+
else (f"{self.code} {self.message}")
|
|
55
|
+
)
|
|
56
|
+
lines = [head]
|
|
57
|
+
lines.extend(f" {d}" for d in self.detail)
|
|
58
|
+
lines.extend(f" fix {i} {fix.title}" for i, fix in enumerate(self.fixes, 1))
|
|
59
|
+
return "\n".join(lines)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Report(BaseModel):
|
|
63
|
+
"""The outcome of a check: an ordered list of diagnostics."""
|
|
64
|
+
|
|
65
|
+
diagnostics: list[Diagnostic] = Field(default_factory=list)
|
|
66
|
+
levels: list[str] = Field(default_factory=list)
|
|
67
|
+
"""Which levels ran: ``static``, ``numeric``, ``smoke``."""
|
|
68
|
+
stats: dict[str, Any] = Field(default_factory=dict)
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def ok(self) -> bool:
|
|
72
|
+
return not any(d.is_error for d in self.diagnostics)
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def errors(self) -> list[Diagnostic]:
|
|
76
|
+
return [d for d in self.diagnostics if d.is_error]
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def warnings(self) -> list[Diagnostic]:
|
|
80
|
+
return [d for d in self.diagnostics if d.severity is Severity.WARNING]
|
|
81
|
+
|
|
82
|
+
def extend(self, other: Report) -> None:
|
|
83
|
+
self.diagnostics.extend(other.diagnostics)
|
|
84
|
+
self.levels.extend(level for level in other.levels if level not in self.levels)
|
|
85
|
+
self.stats.update(other.stats)
|
|
86
|
+
|
|
87
|
+
def summary(self) -> str:
|
|
88
|
+
n_err, n_warn = len(self.errors), len(self.warnings)
|
|
89
|
+
verdict = "ok" if self.ok else "blocked"
|
|
90
|
+
head = f"check {verdict}: {n_err} error(s), {n_warn} warning(s) [{', '.join(self.levels)}]"
|
|
91
|
+
if not self.diagnostics:
|
|
92
|
+
return head
|
|
93
|
+
return head + "\n" + "\n".join(d.render() for d in self.diagnostics)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class Code(StrEnum):
|
|
97
|
+
"""The diagnostic catalogue. Keep in sync with the docs."""
|
|
98
|
+
|
|
99
|
+
# static errors
|
|
100
|
+
UNRESOLVED_ADDRESS = "E101"
|
|
101
|
+
SHAPE_MISMATCH = "E102"
|
|
102
|
+
OP_INVALID_FOR_ROLE = "E103"
|
|
103
|
+
OUTSIDE_SCOPE = "E104"
|
|
104
|
+
NO_OP = "E105"
|
|
105
|
+
BASE_MISMATCH = "E106"
|
|
106
|
+
NON_FINITE = "E107"
|
|
107
|
+
DTYPE_OVERFLOW = "E108"
|
|
108
|
+
QUANTIZED_NEEDS_ADAPTER = "E109"
|
|
109
|
+
# static warnings
|
|
110
|
+
TIED_WEIGHT = "W101"
|
|
111
|
+
READER_NOT_WRITER = "W102"
|
|
112
|
+
REQUANTIZED = "W103"
|
|
113
|
+
FUSED_SPLIT = "W104"
|
|
114
|
+
# numeric warnings
|
|
115
|
+
REL_NORM = "W201"
|
|
116
|
+
SPECTRUM_SHIFT = "W202"
|
|
117
|
+
SUB_ULP = "W203"
|
|
118
|
+
DENSE_EDIT = "W204"
|
|
119
|
+
# smoke warnings
|
|
120
|
+
CANARY_KL = "W301"
|
|
121
|
+
PERPLEXITY = "W302"
|
|
122
|
+
DEGENERATE_OUTPUT = "W303"
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def diag(
|
|
126
|
+
code: Code,
|
|
127
|
+
message: str,
|
|
128
|
+
*,
|
|
129
|
+
target: str | None = None,
|
|
130
|
+
severity: Severity | None = None,
|
|
131
|
+
detail: list[str] | None = None,
|
|
132
|
+
fixes: list[Fix] | None = None,
|
|
133
|
+
**data: Any,
|
|
134
|
+
) -> Diagnostic:
|
|
135
|
+
"""Build a diagnostic; severity defaults from the code's prefix."""
|
|
136
|
+
if severity is None:
|
|
137
|
+
severity = Severity.ERROR if code.value.startswith("E") else Severity.WARNING
|
|
138
|
+
return Diagnostic(
|
|
139
|
+
code=code.value,
|
|
140
|
+
severity=severity,
|
|
141
|
+
message=message,
|
|
142
|
+
target=target,
|
|
143
|
+
detail=detail or [],
|
|
144
|
+
fixes=fixes or [],
|
|
145
|
+
data=data,
|
|
146
|
+
)
|
wspx/driver/__init__.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""The driver runtime (§5.9): wspx's own thin agent loop over the official SDKs.
|
|
2
|
+
|
|
3
|
+
The runner holds no provider keys: every SDK client points at the LLM gateway
|
|
4
|
+
through ``base_url`` and carries a session token. Loop detection and any
|
|
5
|
+
user-set limits are enforced there and in the control plane, so a bug in the
|
|
6
|
+
loop cannot bypass them.
|
|
7
|
+
"""
|
wspx/driver/loop.py
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"""The agent loop (§5.9).
|
|
2
|
+
|
|
3
|
+
Each turn: check the gateway's state for this session (halt, pause, nudges),
|
|
4
|
+
apply any human commands (pause, steer, stop), call the model, record the
|
|
5
|
+
turn, run its tool calls and feed back the results. The runner never spends
|
|
6
|
+
on its own authority: limits and loop stops are enforced outside it.
|
|
7
|
+
|
|
8
|
+
Context stays small at the source (tools return budgeted summaries). Near the
|
|
9
|
+
context limit older turns are compacted into a summary; the workspace table
|
|
10
|
+
(live handles, branch and HEAD, latest check and test results) is re-injected
|
|
11
|
+
after compaction and every few turns, so the model always knows what each
|
|
12
|
+
handle holds.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import re
|
|
18
|
+
import time
|
|
19
|
+
from collections.abc import Callable
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from typing import Any, Literal
|
|
22
|
+
|
|
23
|
+
from wspx.driver.messages import Message, ToolOutput, Turn
|
|
24
|
+
from wspx.driver.providers import Provider
|
|
25
|
+
|
|
26
|
+
Status = Literal["done", "stopped", "paused", "refused", "halted", "error"]
|
|
27
|
+
_CODE_RE = re.compile(r"\b([EW]\d{3})\b")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class ToolReply:
|
|
32
|
+
text: str
|
|
33
|
+
ok: bool = True
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class Hooks:
|
|
38
|
+
"""How the runner talks to the rest of wspx."""
|
|
39
|
+
|
|
40
|
+
tool: Callable[[str, dict[str, Any], str], ToolReply]
|
|
41
|
+
"""(tool name, arguments, call id) → result."""
|
|
42
|
+
gateway_state: Callable[[], dict[str, Any]] = lambda: {}
|
|
43
|
+
"""``{"halted", "paused", "nudges"}`` from the gateway."""
|
|
44
|
+
commands: Callable[[], list[dict[str, Any]]] = lambda: []
|
|
45
|
+
"""Human commands: ``{"type": "stop" | "pause" | "inject", "text"?}``."""
|
|
46
|
+
workspace: Callable[[], str] = lambda: ""
|
|
47
|
+
event: Callable[[dict[str, Any]], None] = lambda e: None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass
|
|
51
|
+
class RunnerConfig:
|
|
52
|
+
system: str
|
|
53
|
+
task: str
|
|
54
|
+
effort: str = "high"
|
|
55
|
+
max_tokens: int = 64_000
|
|
56
|
+
compact_at: int = 600_000
|
|
57
|
+
"""Input tokens per request at which older turns are compacted."""
|
|
58
|
+
keep_recent: int = 6
|
|
59
|
+
workspace_every: int = 10
|
|
60
|
+
stuck_after: int = 3
|
|
61
|
+
max_turns: int | None = None
|
|
62
|
+
"""No default: long experiments are never cut off by the runner."""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class RunResult:
|
|
67
|
+
status: Status
|
|
68
|
+
turns: int
|
|
69
|
+
reason: str = ""
|
|
70
|
+
final_text: str = ""
|
|
71
|
+
history: list[Message] = field(default_factory=list)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class Runner:
|
|
75
|
+
def __init__(self, provider: Provider, hooks: Hooks, config: RunnerConfig) -> None:
|
|
76
|
+
self.provider = provider
|
|
77
|
+
self.hooks = hooks
|
|
78
|
+
self.config = config
|
|
79
|
+
self.history: list[Message] = []
|
|
80
|
+
self.turns = 0
|
|
81
|
+
self._recent: list[tuple[str, tuple[str, ...]]] = []
|
|
82
|
+
self._tools = provider.tools()
|
|
83
|
+
|
|
84
|
+
def run(self) -> RunResult:
|
|
85
|
+
workspace = self.hooks.workspace()
|
|
86
|
+
first = self.config.task + (f"\n\nWorkspace:\n{workspace}" if workspace else "")
|
|
87
|
+
self.history = [Message("user", text=first)]
|
|
88
|
+
pending: list[str] = []
|
|
89
|
+
while True:
|
|
90
|
+
state = self.hooks.gateway_state() or {}
|
|
91
|
+
if state.get("halted"):
|
|
92
|
+
return self._end("halted", f"wspx is halted: {state['halted']}")
|
|
93
|
+
if state.get("paused"):
|
|
94
|
+
return self._end("paused", str(state["paused"]))
|
|
95
|
+
pending.extend(state.get("nudges") or [])
|
|
96
|
+
for cmd in self.hooks.commands():
|
|
97
|
+
if cmd.get("type") == "stop":
|
|
98
|
+
return self._end("stopped", "stopped by a human")
|
|
99
|
+
if cmd.get("type") == "pause":
|
|
100
|
+
return self._end("paused", "paused by a human")
|
|
101
|
+
if cmd.get("type") == "inject" and cmd.get("text"):
|
|
102
|
+
pending.append(f"Message from the human supervising you: {cmd['text']}")
|
|
103
|
+
if self.config.max_turns is not None and self.turns >= self.config.max_turns:
|
|
104
|
+
return self._end(
|
|
105
|
+
"stopped", f"reached the turn limit the user set ({self.config.max_turns})"
|
|
106
|
+
)
|
|
107
|
+
if pending:
|
|
108
|
+
self.history[-1].notes.extend(pending)
|
|
109
|
+
pending = []
|
|
110
|
+
|
|
111
|
+
try:
|
|
112
|
+
turn = self.provider.step(
|
|
113
|
+
self.config.system,
|
|
114
|
+
self.history,
|
|
115
|
+
self._tools,
|
|
116
|
+
effort=self.config.effort,
|
|
117
|
+
max_tokens=self.config.max_tokens,
|
|
118
|
+
)
|
|
119
|
+
except Exception as exc:
|
|
120
|
+
self.hooks.event(
|
|
121
|
+
{"type": "error", "turn": self.turns, "error": f"{type(exc).__name__}: {exc}"}
|
|
122
|
+
)
|
|
123
|
+
return self._end("error", f"{type(exc).__name__}: {exc}")
|
|
124
|
+
self.turns += 1
|
|
125
|
+
self.history.append(
|
|
126
|
+
Message(
|
|
127
|
+
"assistant",
|
|
128
|
+
text=turn.text,
|
|
129
|
+
native=turn.native,
|
|
130
|
+
provider=self.provider.name,
|
|
131
|
+
tool_calls=turn.tool_calls,
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
self._emit_turn(turn)
|
|
135
|
+
if turn.stop == "refusal":
|
|
136
|
+
return self._end("refused", "the model declined (refusal)", turn.text)
|
|
137
|
+
if not turn.tool_calls:
|
|
138
|
+
return self._end("done", "the model finished", turn.text)
|
|
139
|
+
|
|
140
|
+
outputs = []
|
|
141
|
+
for call in turn.tool_calls:
|
|
142
|
+
started = time.perf_counter()
|
|
143
|
+
reply = self.hooks.tool(call.name, call.args, call.id)
|
|
144
|
+
self.hooks.event(
|
|
145
|
+
{
|
|
146
|
+
"type": "tool_call",
|
|
147
|
+
"turn": self.turns,
|
|
148
|
+
"call_id": call.id,
|
|
149
|
+
"tool": call.name,
|
|
150
|
+
"args": call.args,
|
|
151
|
+
"ok": reply.ok,
|
|
152
|
+
"summary": reply.text[:2000],
|
|
153
|
+
"latency": time.perf_counter() - started,
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
outputs.append(ToolOutput(call.id, call.name, reply.text, is_error=not reply.ok))
|
|
157
|
+
pending.extend(self._stuck(call.name, reply.text))
|
|
158
|
+
message = Message("tool", outputs=outputs)
|
|
159
|
+
self.history.append(message)
|
|
160
|
+
if (
|
|
161
|
+
turn.usage.input + turn.usage.cache_read + turn.usage.cache_write
|
|
162
|
+
> self.config.compact_at
|
|
163
|
+
):
|
|
164
|
+
self._compact()
|
|
165
|
+
pending.append("Workspace:\n" + self.hooks.workspace())
|
|
166
|
+
elif self.turns % self.config.workspace_every == 0:
|
|
167
|
+
table = self.hooks.workspace()
|
|
168
|
+
if table:
|
|
169
|
+
pending.append("Workspace:\n" + table)
|
|
170
|
+
|
|
171
|
+
# ------------------------------------------------------------------ helpers
|
|
172
|
+
|
|
173
|
+
def _emit_turn(self, turn: Turn) -> None:
|
|
174
|
+
self.hooks.event(
|
|
175
|
+
{
|
|
176
|
+
"type": "turn",
|
|
177
|
+
"turn": self.turns,
|
|
178
|
+
"provider": self.provider.name,
|
|
179
|
+
"model": turn.model,
|
|
180
|
+
"text": turn.text,
|
|
181
|
+
"reasoning": turn.reasoning,
|
|
182
|
+
"reasoning_available": turn.reasoning is not None,
|
|
183
|
+
"tool_calls": [
|
|
184
|
+
{"id": c.id, "name": c.name, "args": c.args} for c in turn.tool_calls
|
|
185
|
+
],
|
|
186
|
+
"usage": turn.usage.__dict__,
|
|
187
|
+
"stop": turn.stop,
|
|
188
|
+
"latency": turn.latency,
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
def _stuck(self, tool: str, text: str) -> list[str]:
|
|
193
|
+
"""The same tool returning the same diagnostic codes N times running: surface the fixes."""
|
|
194
|
+
codes = tuple(sorted(set(_CODE_RE.findall(text))))
|
|
195
|
+
if not codes:
|
|
196
|
+
self._recent.clear()
|
|
197
|
+
return []
|
|
198
|
+
self._recent.append((tool, codes))
|
|
199
|
+
n = self.config.stuck_after
|
|
200
|
+
if len(self._recent) >= n and len(set(self._recent[-n:])) == 1:
|
|
201
|
+
fixes = [line.strip() for line in text.splitlines() if line.strip().startswith("fix ")]
|
|
202
|
+
self._recent.clear()
|
|
203
|
+
listing = "\n".join(fixes) or "(see the diagnostic above)"
|
|
204
|
+
return [
|
|
205
|
+
f"{tool} returned {', '.join(codes)} {n} times in a row; retrying will not change it. "
|
|
206
|
+
f"Apply one of the listed fixes:\n{listing}"
|
|
207
|
+
]
|
|
208
|
+
return []
|
|
209
|
+
|
|
210
|
+
def _compact(self) -> None:
|
|
211
|
+
"""Replace older turns with a summary; handles and the commit log stay authoritative."""
|
|
212
|
+
keep = self.config.keep_recent
|
|
213
|
+
if len(self.history) <= keep + 1:
|
|
214
|
+
return
|
|
215
|
+
old, recent = self.history[:-keep], self.history[-keep:]
|
|
216
|
+
while recent and recent[0].role == "tool": # never start on orphaned tool results
|
|
217
|
+
old.append(recent.pop(0))
|
|
218
|
+
transcript = []
|
|
219
|
+
for msg in old:
|
|
220
|
+
if msg.role == "assistant":
|
|
221
|
+
calls = ", ".join(f"{c.name}({c.args})" for c in msg.tool_calls)
|
|
222
|
+
transcript.append(
|
|
223
|
+
f"assistant: {msg.text[:2000]} {('calls: ' + calls) if calls else ''}"
|
|
224
|
+
)
|
|
225
|
+
else:
|
|
226
|
+
for o in msg.outputs:
|
|
227
|
+
transcript.append(f"{o.name} → {o.content[:1500]}")
|
|
228
|
+
if msg.text:
|
|
229
|
+
transcript.append(f"user: {msg.text[:4000]}")
|
|
230
|
+
prompt = (
|
|
231
|
+
"Summarise the work so far for yourself: the goal, what was found (with addresses and "
|
|
232
|
+
"handles), what was edited and committed, what was tested and the results, and what is "
|
|
233
|
+
"left to do. Be specific and brief.\n\n" + "\n".join(transcript)
|
|
234
|
+
)
|
|
235
|
+
summary = self.provider.step(
|
|
236
|
+
self.config.system, [Message("user", text=prompt)], [], effort="low", max_tokens=4_000
|
|
237
|
+
)
|
|
238
|
+
self.hooks.event(
|
|
239
|
+
{
|
|
240
|
+
"type": "compaction",
|
|
241
|
+
"turn": self.turns,
|
|
242
|
+
"summary": summary.text,
|
|
243
|
+
"dropped_messages": len(old),
|
|
244
|
+
}
|
|
245
|
+
)
|
|
246
|
+
head = Message(
|
|
247
|
+
"user", text=f"{self.config.task}\n\nSummary of earlier work:\n{summary.text}"
|
|
248
|
+
)
|
|
249
|
+
self.history = [head, *recent]
|
|
250
|
+
if self.history[1:2] and self.history[1].role == "user":
|
|
251
|
+
head.notes.extend(self.history[1].notes)
|
|
252
|
+
|
|
253
|
+
def _end(self, status: Status, reason: str, text: str = "") -> RunResult:
|
|
254
|
+
self.hooks.event({"type": "end", "status": status, "reason": reason, "turns": self.turns})
|
|
255
|
+
return RunResult(status, self.turns, reason, text, self.history)
|
wspx/driver/messages.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Provider-neutral conversation records.
|
|
2
|
+
|
|
3
|
+
Assistant turns keep the provider's native output (``native``) and replay it
|
|
4
|
+
unchanged, so reasoning state round-trips exactly: Anthropic thinking blocks
|
|
5
|
+
with their signatures, OpenAI reasoning items with encrypted content, Gemini
|
|
6
|
+
parts with thought signatures.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from typing import Any, Literal
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class ToolCall:
|
|
17
|
+
id: str
|
|
18
|
+
name: str
|
|
19
|
+
args: dict[str, Any]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class ToolOutput:
|
|
24
|
+
call_id: str
|
|
25
|
+
name: str
|
|
26
|
+
content: str
|
|
27
|
+
is_error: bool = False
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class Message:
|
|
32
|
+
role: Literal["user", "assistant", "tool"]
|
|
33
|
+
text: str = ""
|
|
34
|
+
native: Any = None
|
|
35
|
+
"""Provider-native assistant output, replayed as-is."""
|
|
36
|
+
provider: str | None = None
|
|
37
|
+
tool_calls: list[ToolCall] = field(default_factory=list)
|
|
38
|
+
outputs: list[ToolOutput] = field(default_factory=list)
|
|
39
|
+
notes: list[str] = field(default_factory=list)
|
|
40
|
+
"""Operator notes (nudges, workspace table, human steering) for the model."""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass
|
|
44
|
+
class Usage:
|
|
45
|
+
input: int = 0
|
|
46
|
+
output: int = 0
|
|
47
|
+
cache_read: int = 0
|
|
48
|
+
cache_write: int = 0
|
|
49
|
+
reasoning: int = 0
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class Turn:
|
|
54
|
+
text: str
|
|
55
|
+
reasoning: str | None
|
|
56
|
+
"""Reasoning as the API returned it (summaries); ``None`` when the provider gives none."""
|
|
57
|
+
tool_calls: list[ToolCall]
|
|
58
|
+
usage: Usage
|
|
59
|
+
stop: str
|
|
60
|
+
native: Any
|
|
61
|
+
model: str = ""
|
|
62
|
+
latency: float = 0.0
|