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
wspx/vcs/repo.py ADDED
@@ -0,0 +1,431 @@
1
+ """The repository: commits, trees and patch records over a :class:`Store`."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ import time
7
+ from collections.abc import Iterator
8
+ from dataclasses import dataclass, field
9
+ from typing import Any
10
+
11
+ from wspx.addressing import is_valid_ref_name
12
+ from wspx.errors import StoreError, VCSError
13
+ from wspx.store import Store, is_oid, short
14
+
15
+ HEADS = "heads/"
16
+ _REV_RE = re.compile(r"^(?P<base>[^~^]+)(?P<ops>(?:~\d*|\^\d*)*)$")
17
+
18
+
19
+ def short_id(oid: str) -> str:
20
+ return short(oid, 7)
21
+
22
+
23
+ @dataclass
24
+ class PatchRecord:
25
+ """A staged edit. Its id (``p_<8 hex>``) is derived from its content hash."""
26
+
27
+ op: str
28
+ args: dict[str, Any]
29
+ precision: str
30
+ deltas: list[dict[str, Any]]
31
+ """``[{"tensor", "kind", "blob", "rank"?}]``."""
32
+ created: float = field(default_factory=time.time)
33
+ call: str | None = None
34
+ script: str | None = None
35
+ """Hash of the script that produced it, when it came from ``run``."""
36
+ oid: str = ""
37
+
38
+ @property
39
+ def handle(self) -> str:
40
+ return "p_" + short(self.oid, 8)
41
+
42
+ @property
43
+ def tensors(self) -> list[str]:
44
+ return sorted({d["tensor"] for d in self.deltas})
45
+
46
+ def to_dict(self) -> dict[str, Any]:
47
+ out = {
48
+ "type": "patch",
49
+ "op": {"name": self.op, "args": self.args},
50
+ "precision": self.precision,
51
+ "deltas": self.deltas,
52
+ "created": self.created,
53
+ }
54
+ if self.call:
55
+ out["call"] = self.call
56
+ if self.script:
57
+ out["script"] = self.script
58
+ return out
59
+
60
+ @classmethod
61
+ def from_dict(cls, data: dict[str, Any], oid: str = "") -> PatchRecord:
62
+ return cls(
63
+ op=data["op"]["name"],
64
+ args=data["op"].get("args", {}),
65
+ precision=data.get("precision", "merged"),
66
+ deltas=list(data["deltas"]),
67
+ created=data.get("created", 0.0),
68
+ call=data.get("call"),
69
+ script=data.get("script"),
70
+ oid=oid,
71
+ )
72
+
73
+
74
+ @dataclass
75
+ class Commit:
76
+ parents: list[str]
77
+ base: str
78
+ tree: str
79
+ patches: list[str]
80
+ """Patch-record ids introduced by this commit."""
81
+ result_hashes: dict[str, str]
82
+ message: str
83
+ author: dict[str, Any] = field(default_factory=dict)
84
+ checks: str | None = None
85
+ evals: list[str] = field(default_factory=list)
86
+ time: float = field(default_factory=time.time)
87
+ forced: bool = False
88
+ merge: dict[str, Any] | None = None
89
+ status: str = "ok"
90
+ """``ok`` or ``needs-review`` (a merge whose interference check failed)."""
91
+ oid: str = ""
92
+
93
+ @property
94
+ def short(self) -> str:
95
+ return short_id(self.oid)
96
+
97
+ def to_dict(self) -> dict[str, Any]:
98
+ out: dict[str, Any] = {
99
+ "type": "commit",
100
+ "parents": self.parents,
101
+ "base": self.base,
102
+ "tree": self.tree,
103
+ "patches": self.patches,
104
+ "result_hashes": self.result_hashes,
105
+ "message": self.message,
106
+ "author": self.author,
107
+ "evals": self.evals,
108
+ "time": self.time,
109
+ "status": self.status,
110
+ }
111
+ if self.checks:
112
+ out["checks"] = self.checks
113
+ if self.forced:
114
+ out["forced"] = True
115
+ if self.merge is not None:
116
+ out["merge"] = self.merge
117
+ return out
118
+
119
+ @classmethod
120
+ def from_dict(cls, data: dict[str, Any], oid: str) -> Commit:
121
+ if data.get("type") != "commit":
122
+ raise VCSError(f"{short_id(oid)} is a {data.get('type')}, not a commit")
123
+ return cls(
124
+ parents=list(data["parents"]),
125
+ base=data["base"],
126
+ tree=data["tree"],
127
+ patches=list(data.get("patches", [])),
128
+ result_hashes=dict(data.get("result_hashes", {})),
129
+ message=data.get("message", ""),
130
+ author=data.get("author", {}),
131
+ checks=data.get("checks"),
132
+ evals=list(data.get("evals", [])),
133
+ time=data.get("time", 0.0),
134
+ forced=data.get("forced", False),
135
+ merge=data.get("merge"),
136
+ status=data.get("status", "ok"),
137
+ oid=oid,
138
+ )
139
+
140
+
141
+ EMPTY_TREE: dict[str, Any] = {"type": "tree", "merged": {}, "adapter": {}, "promote": []}
142
+
143
+
144
+ def normalize_tree(tree: dict[str, Any] | None) -> dict[str, Any]:
145
+ """The canonical form of a tree: sorted, empty entries dropped, no type tag."""
146
+ tree = tree or {}
147
+ return {
148
+ "merged": {k: sorted(v) for k, v in sorted((tree.get("merged") or {}).items()) if v},
149
+ "adapter": {k: sorted(v) for k, v in sorted((tree.get("adapter") or {}).items()) if v},
150
+ "promote": sorted(tree.get("promote") or []),
151
+ }
152
+
153
+
154
+ def tree_key(tree: dict[str, Any] | None) -> str:
155
+ """Short hash of the weights a tree describes: the eval-cache key (same weights, same key)."""
156
+ import hashlib
157
+
158
+ from wspx.store import canonical_json
159
+
160
+ return hashlib.sha256(canonical_json(normalize_tree(tree))).hexdigest()[:16]
161
+
162
+
163
+ class Repo:
164
+ def __init__(self, store: Store, base: str) -> None:
165
+ self.store = store
166
+ self.base = base
167
+ self._commits: dict[str, Commit] = {}
168
+
169
+ # ------------------------------------------------------------------ objects
170
+
171
+ def put_tree(self, tree: dict[str, Any]) -> str:
172
+ return self.store.objects.put_json({**EMPTY_TREE, **tree, "type": "tree"})
173
+
174
+ def get_tree(self, oid: str | None) -> dict[str, Any]:
175
+ if oid is None:
176
+ return dict(EMPTY_TREE)
177
+ data = self.store.objects.get_json(oid)
178
+ if data.get("type") != "tree":
179
+ raise VCSError(f"{short_id(oid)} is not a tree")
180
+ return dict(data)
181
+
182
+ def put_patch(self, record: PatchRecord) -> PatchRecord:
183
+ record.oid = self.store.objects.put_json(record.to_dict())
184
+ return record
185
+
186
+ def get_patch(self, ref: str) -> PatchRecord:
187
+ oid = self._expand(ref.removeprefix("p_"))
188
+ return PatchRecord.from_dict(self.store.objects.get_json(oid), oid)
189
+
190
+ def get_commit(self, oid: str) -> Commit:
191
+ if oid not in self._commits:
192
+ self._commits[oid] = Commit.from_dict(self.store.objects.get_json(oid), oid)
193
+ return self._commits[oid]
194
+
195
+ def tree_of(self, rev: str | None) -> dict[str, Any]:
196
+ if rev is None:
197
+ return dict(EMPTY_TREE)
198
+ oid = self.resolve(rev)
199
+ return self.get_tree(self.get_commit(oid).tree) if oid else dict(EMPTY_TREE)
200
+
201
+ # ------------------------------------------------------------------ refs
202
+
203
+ def head_ref(self, session: str) -> str:
204
+ return f"sessions/{session}/HEAD"
205
+
206
+ def init_session(
207
+ self, session: str, branch: str | None = None, *, start: str | None = None
208
+ ) -> str:
209
+ """Point the session's HEAD at ``branch`` (created at ``start`` if missing)."""
210
+ branch = branch or session
211
+ self._check_branch(branch)
212
+ ref = HEADS + branch
213
+ if start is not None and self.store.refs.read(ref) is None:
214
+ self.store.refs.update(ref, self.resolve(start) or "", expected=None)
215
+ self.store.refs.set_symbolic(self.head_ref(session), ref)
216
+ return branch
217
+
218
+ def current_branch(self, session: str) -> str | None:
219
+ target = self.store.refs.symbolic_target(self.head_ref(session))
220
+ return target.removeprefix(HEADS) if target and target.startswith(HEADS) else None
221
+
222
+ def head(self, session: str) -> str | None:
223
+ value = self.store.refs.read(self.head_ref(session))
224
+ return value or None
225
+
226
+ def branches(self) -> dict[str, str]:
227
+ return {k.removeprefix(HEADS): v for k, v in self.store.refs.list(HEADS).items() if v}
228
+
229
+ def create_branch(self, name: str, at: str | None) -> str:
230
+ self._check_branch(name)
231
+ oid = self.resolve(at) if at else None
232
+ if self.store.refs.exists(HEADS + name):
233
+ raise VCSError(f"branch {name!r} already exists")
234
+ self.store.refs.update(HEADS + name, oid or "", expected=None)
235
+ return oid or ""
236
+
237
+ def delete_branch(self, name: str) -> None:
238
+ self.store.refs.delete(HEADS + name)
239
+
240
+ def switch(self, session: str, branch: str) -> str | None:
241
+ if not self.store.refs.exists(HEADS + branch):
242
+ raise VCSError(f"no branch {branch!r}")
243
+ self.store.refs.set_symbolic(self.head_ref(session), HEADS + branch)
244
+ return self.head(session)
245
+
246
+ def detach(self, session: str, oid: str) -> None:
247
+ self.store.refs.delete(self.head_ref(session))
248
+ self.store.refs.update(self.head_ref(session), oid)
249
+
250
+ @staticmethod
251
+ def _check_branch(name: str) -> None:
252
+ if not is_valid_ref_name(name) or name.startswith(("p_", "t_", "HEAD")):
253
+ raise VCSError(f"invalid branch name {name!r}")
254
+
255
+ # ------------------------------------------------------------------ commit
256
+
257
+ def commit(
258
+ self,
259
+ session: str,
260
+ *,
261
+ tree: dict[str, Any],
262
+ patches: list[str],
263
+ result_hashes: dict[str, str],
264
+ message: str,
265
+ author: dict[str, Any] | None = None,
266
+ checks: str | None = None,
267
+ evals: list[str] | None = None,
268
+ forced: bool = False,
269
+ extra_parents: list[str] | None = None,
270
+ merge: dict[str, Any] | None = None,
271
+ status: str = "ok",
272
+ ) -> Commit:
273
+ if not message.strip():
274
+ raise VCSError("commit message is empty")
275
+ parent = self.head(session)
276
+ parents = ([parent] if parent else []) + list(extra_parents or [])
277
+ commit = Commit(
278
+ parents=parents,
279
+ base=self.base,
280
+ tree=self.put_tree(tree),
281
+ patches=patches,
282
+ result_hashes=result_hashes,
283
+ message=message.strip(),
284
+ author=author or {},
285
+ checks=checks,
286
+ evals=evals or [],
287
+ forced=forced,
288
+ merge=merge,
289
+ status=status,
290
+ )
291
+ commit.oid = self.store.objects.put_json(commit.to_dict())
292
+ self.store.refs.update(self.head_ref(session), commit.oid, expected=parent)
293
+ self._commits[commit.oid] = commit
294
+ return commit
295
+
296
+ # ------------------------------------------------------------------ revisions
297
+
298
+ def resolve(self, rev: str, session: str | None = None) -> str | None:
299
+ """``HEAD``, ``HEAD~2``, ``HEAD^2``, a branch, a (short) commit id or ``base``."""
300
+ rev = rev.strip()
301
+ if rev in ("base", "BASE"):
302
+ return None
303
+ m = _REV_RE.match(rev)
304
+ if not m:
305
+ raise VCSError(f"bad revision {rev!r}")
306
+ oid = self._resolve_base(m.group("base"), session)
307
+ for op in re.findall(r"[~^]\d*", m.group("ops")):
308
+ n = int(op[1:]) if len(op) > 1 else 1
309
+ if oid is None:
310
+ raise VCSError(f"{rev}: walks past the base")
311
+ if op[0] == "~":
312
+ for _ in range(n):
313
+ if oid is None:
314
+ raise VCSError(f"{rev}: walks past the base")
315
+ parents = self.get_commit(oid).parents
316
+ oid = parents[0] if parents else None # the root's parent is the base
317
+ else:
318
+ parents = self.get_commit(oid).parents
319
+ if n == 0:
320
+ continue
321
+ if n > len(parents):
322
+ raise VCSError(f"{rev}: commit has {len(parents)} parent(s)")
323
+ oid = parents[n - 1]
324
+ return oid
325
+
326
+ def _resolve_base(self, name: str, session: str | None) -> str | None:
327
+ if name == "HEAD":
328
+ if session is None:
329
+ raise VCSError("HEAD needs a session")
330
+ return self.head(session)
331
+ if is_valid_ref_name(name) and self.store.refs.exists(HEADS + name):
332
+ return self.store.refs.read(HEADS + name)
333
+ if is_oid(name):
334
+ return name
335
+ try:
336
+ return self._expand(name)
337
+ except StoreError:
338
+ raise VCSError(f"unknown revision {name!r}") from None
339
+
340
+ def _expand(self, prefix: str) -> str:
341
+ return self.store.objects.resolve_prefix(prefix)
342
+
343
+ # ------------------------------------------------------------------ history
344
+
345
+ def walk(self, start: str | None, *, first_parent: bool = False) -> Iterator[Commit]:
346
+ """Commits reachable from ``start``, newest first."""
347
+ seen: set[str] = set()
348
+ frontier = [start] if start else []
349
+ while frontier:
350
+ frontier.sort(key=lambda o: -self.get_commit(o).time)
351
+ oid = frontier.pop(0)
352
+ if oid in seen:
353
+ continue
354
+ seen.add(oid)
355
+ commit = self.get_commit(oid)
356
+ yield commit
357
+ parents = commit.parents[:1] if first_parent else commit.parents
358
+ frontier.extend(p for p in parents if p not in seen)
359
+
360
+ def log(self, rev: str | None, n: int = 20) -> list[Commit]:
361
+ out = []
362
+ for commit in self.walk(rev):
363
+ out.append(commit)
364
+ if len(out) >= n:
365
+ break
366
+ return out
367
+
368
+ def merge_base(self, a: str | None, b: str | None) -> str | None:
369
+ if a is None or b is None:
370
+ return None
371
+ ancestors = {c.oid for c in self.walk(a)}
372
+ for commit in self.walk(b):
373
+ if commit.oid in ancestors:
374
+ return commit.oid
375
+ return None
376
+
377
+ def is_ancestor(self, a: str | None, b: str | None) -> bool:
378
+ if a is None:
379
+ return True
380
+ return b is not None and any(c.oid == a for c in self.walk(b))
381
+
382
+ def introduced(self, since: str | None, until: str | None) -> dict[str, list[str]]:
383
+ """Delta blobs per tensor in ``until``'s tree that are not in ``since``'s tree."""
384
+ before = self.get_tree(self.get_commit(since).tree) if since else EMPTY_TREE
385
+ after = self.get_tree(self.get_commit(until).tree) if until else EMPTY_TREE
386
+ out: dict[str, list[str]] = {}
387
+ for section in ("merged", "adapter"):
388
+ for tensor, blobs in after.get(section, {}).items():
389
+ old = set(before.get(section, {}).get(tensor, []))
390
+ new = [b for b in blobs if b not in old]
391
+ if new:
392
+ out.setdefault(tensor, []).extend(new)
393
+ return out
394
+
395
+ def revert_tree(self, session: str, rev: str) -> dict[str, Any]:
396
+ """HEAD's tree without the deltas ``rev`` introduced (exact: sums restart from base)."""
397
+ oid = self.resolve(rev, session)
398
+ if oid is None:
399
+ raise VCSError("nothing to revert at the base")
400
+ commit = self.get_commit(oid)
401
+ removed = self.introduced(commit.parents[0] if commit.parents else None, oid)
402
+ tree = self.tree_of_oid(self.head(session))
403
+ for section in ("merged", "adapter"):
404
+ for tensor, blobs in list(tree.get(section, {}).items()):
405
+ keep = [b for b in blobs if b not in set(removed.get(tensor, []))]
406
+ if keep:
407
+ tree[section][tensor] = keep
408
+ else:
409
+ del tree[section][tensor]
410
+ touched_left = set(tree.get("merged", {})) | set(tree.get("adapter", {}))
411
+ tree["promote"] = [t for t in tree.get("promote", []) if t in touched_left]
412
+ return tree
413
+
414
+ def tree_of_oid(self, oid: str | None) -> dict[str, Any]:
415
+ if oid is None:
416
+ return dict(EMPTY_TREE)
417
+ return {
418
+ k: (dict(v) if isinstance(v, dict) else v)
419
+ for k, v in self.get_tree(self.get_commit(oid).tree).items()
420
+ }
421
+
422
+ def blame(self, start: str | None, tensor: str, touches: Any) -> list[Commit]:
423
+ """Commits whose patches touched ``tensor`` where ``touches(delta_entry)`` says so."""
424
+ out = []
425
+ for commit in self.walk(start):
426
+ for pid in commit.patches:
427
+ record = self.get_patch(pid)
428
+ if any(d["tensor"] == tensor and touches(d) for d in record.deltas):
429
+ out.append(commit)
430
+ break
431
+ return out