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,424 @@
1
+ """Sessions and their lifecycle (§3.2, §5.13).
2
+
3
+ Activity is defined by the session, not by lab traffic: a session is active
4
+ while its driver is running (thinking counts) or someone has it open in the
5
+ UI. Active sessions keep their lab hot and its lease renewed; idle ones keep
6
+ it for the grace period, then release the GPU. Store-served tools answer
7
+ while the lab is cold, and a lab-bound call wakes it.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import contextlib
13
+ import json
14
+ import re
15
+ import secrets
16
+ import threading
17
+ import time
18
+ from collections import defaultdict, deque
19
+ from dataclasses import fields
20
+ from typing import Any
21
+
22
+ from wspx.auth import mint
23
+ from wspx.control.backend import Backend, LabHandle, RunnerHandle, SessionRecord
24
+ from wspx.control.gateway_client import GatewayAdmin
25
+ from wspx.control.tracedb import TraceDB
26
+ from wspx.errors import WspxError
27
+ from wspx.store import Store, short
28
+ from wspx.store.overlay import apply_journal
29
+ from wspx.tools import TOOLS
30
+
31
+ LEASE_S = 300.0
32
+ RECENT_CALL_S = 120.0
33
+ STORE_SERVED = frozenset({"info", "ls"})
34
+ _CODE_RE = re.compile(r"\b([EW]\d{3})\b")
35
+
36
+
37
+ class SessionManager:
38
+ def __init__(
39
+ self,
40
+ store: Store,
41
+ trace: TraceDB,
42
+ backend: Backend,
43
+ *,
44
+ gateway: GatewayAdmin | None = None,
45
+ secret: str = "",
46
+ control_url: str = "",
47
+ gateway_url: str = "",
48
+ ) -> None:
49
+ self.store = store
50
+ self.trace = trace
51
+ self.backend = backend
52
+ self.gateway = gateway
53
+ self.secret = secret
54
+ self.control_url = control_url
55
+ self.gateway_url = gateway_url
56
+ self.records: dict[str, SessionRecord] = {}
57
+ self.labs: dict[str, LabHandle] = {}
58
+ self.runners: dict[str, RunnerHandle] = {}
59
+ self.commands: dict[str, deque[dict[str, Any]]] = defaultdict(deque)
60
+ self.viewers: dict[str, int] = defaultdict(int)
61
+ self.last_activity: dict[str, float] = {}
62
+ self.idle_since: dict[str, float] = {}
63
+ self.workspace: dict[str, str] = {}
64
+ self.halted = False
65
+ self._lab_locks: dict[str, threading.Lock] = defaultdict(threading.Lock)
66
+ self._lock = threading.RLock()
67
+ for data in trace.sessions():
68
+ known = {f.name for f in fields(SessionRecord)}
69
+ record = SessionRecord(**{k: v for k, v in data.items() if k in known})
70
+ record.tier = "cold"
71
+ self.records[record.id] = record
72
+
73
+ # ------------------------------------------------------------------ sessions
74
+
75
+ def create(
76
+ self,
77
+ *,
78
+ model: str,
79
+ driver: str | None = None,
80
+ task: str = "",
81
+ gpu: str | None = None,
82
+ branch: str | None = None,
83
+ revision: str | None = None,
84
+ dtype: str = "bfloat16",
85
+ trust_remote_code: bool = False,
86
+ grace_s: float = 900.0,
87
+ keep_warm: str = "auto",
88
+ project: str = "",
89
+ snapshot: str | None = None,
90
+ ) -> SessionRecord:
91
+ if self.halted:
92
+ raise WspxError("wspx is halted; `wspx resume` first")
93
+ if snapshot is not None and not self.store.objects.has(snapshot):
94
+ raise WspxError(f"project snapshot {snapshot[:12]} was not uploaded")
95
+ sid = "s_" + secrets.token_hex(4)
96
+ record = SessionRecord(
97
+ sid,
98
+ model,
99
+ driver,
100
+ task,
101
+ gpu,
102
+ branch or sid,
103
+ revision,
104
+ dtype,
105
+ trust_remote_code,
106
+ grace_s=grace_s,
107
+ keep_warm=keep_warm,
108
+ project=project,
109
+ snapshot=snapshot,
110
+ created=time.time(),
111
+ )
112
+ self.records[sid] = record
113
+ self._save(record)
114
+ self.trace.event(
115
+ sid, "session", {"status": "created", "model": model, "driver": driver, "task": task}
116
+ )
117
+ self.touch(sid)
118
+ threading.Thread(target=self._prewarm, args=(sid,), daemon=True).start()
119
+ return record
120
+
121
+ def get(self, sid: str) -> SessionRecord:
122
+ if sid not in self.records:
123
+ raise WspxError(f"no session {sid}")
124
+ return self.records[sid]
125
+
126
+ def _save(self, record: SessionRecord) -> None:
127
+ self.trace.put_session(record.to_dict())
128
+
129
+ def _prewarm(self, sid: str) -> None:
130
+ record = self.get(sid)
131
+ try:
132
+ self.lab(sid)
133
+ except Exception as exc:
134
+ record.status, record.reason = "error", f"lab failed to start: {exc}"
135
+ self._save(record)
136
+ self.trace.event(sid, "error", {"error": record.reason})
137
+ return
138
+ if record.driver:
139
+ self.start_driver(sid)
140
+
141
+ def start_driver(self, sid: str) -> None:
142
+ record = self.get(sid)
143
+ if not record.driver:
144
+ raise WspxError(f"{sid} has no driver")
145
+ env = {
146
+ "WSPX_SESSION": sid,
147
+ "WSPX_DRIVER": record.driver,
148
+ "WSPX_TASK": record.task,
149
+ "WSPX_CONTROL_URL": self.control_url,
150
+ "WSPX_GATEWAY_URL": self.gateway_url,
151
+ "WSPX_RUNNER_TOKEN": mint(self.secret, sid, scope="runner"),
152
+ "WSPX_LLM_TOKEN": mint(self.secret, sid, scope="llm"),
153
+ }
154
+ record.status = "running"
155
+ self._save(record)
156
+ self.runners[sid] = self.backend.start_runner(record, env)
157
+ self.trace.event(sid, "session", {"status": "running"})
158
+
159
+ # ------------------------------------------------------------------ labs
160
+
161
+ def lab(self, sid: str) -> LabHandle:
162
+ """The session's lab, started if cold (blocks until it is up)."""
163
+ with self._lab_locks[sid]:
164
+ if sid in self.labs:
165
+ return self.labs[sid]
166
+ record = self.get(sid)
167
+ self.trace.event(sid, "lab", {"tier": "starting"})
168
+ started = time.time()
169
+ handle = self.backend.start_lab(record)
170
+ record.load_seconds = time.time() - started
171
+ record.tier = "hot"
172
+ self.labs[sid] = handle
173
+ self._save(record)
174
+ self._persist(sid, handle.heartbeat(LEASE_S))
175
+ self.trace.event(sid, "lab", {"tier": "hot", "load_seconds": record.load_seconds})
176
+ return handle
177
+
178
+ def stop_lab(self, sid: str, reason: str) -> None:
179
+ with self._lab_locks[sid]:
180
+ handle = self.labs.pop(sid, None)
181
+ if handle is None:
182
+ return
183
+ with contextlib.suppress(Exception):
184
+ self._persist(sid, handle.heartbeat(0))
185
+ handle.stop()
186
+ record = self.get(sid)
187
+ record.tier = "cold"
188
+ self._save(record)
189
+ self.trace.event(sid, "lab", {"tier": "cold", "reason": reason})
190
+
191
+ def _persist(self, sid: str, response: dict[str, Any]) -> None:
192
+ journal = response.get("journal")
193
+ if journal:
194
+ apply_journal(self.store, journal)
195
+ if response.get("workspace"):
196
+ self.workspace[sid] = response["workspace"]
197
+ record = self.records.get(sid)
198
+ fingerprint = response.get("fingerprint")
199
+ if record is not None and fingerprint and record.fingerprint != fingerprint:
200
+ record.fingerprint = fingerprint
201
+ self._save(record)
202
+
203
+ # ------------------------------------------------------------------ tools
204
+
205
+ def tool(
206
+ self,
207
+ sid: str,
208
+ name: str,
209
+ args: dict[str, Any],
210
+ *,
211
+ call_id: str | None = None,
212
+ source: str = "driver",
213
+ turn: int | None = None,
214
+ context: dict[str, Any] | None = None,
215
+ ) -> dict[str, Any]:
216
+ record = self.get(sid)
217
+ if name not in TOOLS:
218
+ return {"summary": f"unknown tool {name!r}", "ok": False, "handles": [], "data": {}}
219
+ if self.halted:
220
+ return {
221
+ "summary": "wspx is halted; `wspx resume` to continue",
222
+ "ok": False,
223
+ "handles": [],
224
+ "data": {},
225
+ }
226
+ call_id = call_id or "c_" + secrets.token_hex(5)
227
+ self.touch(sid)
228
+ started = time.time()
229
+ result = self._store_served(record, name, args) if sid not in self.labs else None
230
+ note = ""
231
+ if result is None:
232
+ if sid not in self.labs:
233
+ note = "(lab cold start {:.0f} s)"
234
+ handle = self.lab(sid)
235
+ ctx = {"caller": source, "driver": record.driver, **(context or {})}
236
+ result = handle.call(name, args, call_id, ctx)
237
+ self._persist(sid, result)
238
+ result.pop("journal", None)
239
+ if note:
240
+ note = note.format(time.time() - started)
241
+ latency = time.time() - started
242
+ result["summary"] = (result.get("summary") or "") + (f"\n{note}" if note else "")
243
+ result["call_id"] = call_id
244
+ result["summary"] += self._observe(sid, name, args, result)
245
+ self.trace.tool_call(
246
+ sid, call_id, name, args, result, turn=turn, latency=latency, source=source
247
+ )
248
+ return result
249
+
250
+ def _store_served(
251
+ self, record: SessionRecord, name: str, args: dict[str, Any]
252
+ ) -> dict[str, Any] | None:
253
+ """Answer from the store while the lab is cold, when the tool allows it."""
254
+ if name not in STORE_SERVED or record.fingerprint is None:
255
+ return None
256
+ raw = self.store.read_file(f"models/{record.fingerprint.replace(':', '_')}/ir.json")
257
+ if raw is None:
258
+ return None
259
+ from wspx.lab.ir.model_ir import ModelIR
260
+
261
+ ir = ModelIR.from_dict(json.loads(raw))
262
+ if name == "info":
263
+ head = self.store.refs.read(f"sessions/{record.id}/HEAD")
264
+ return {
265
+ "summary": ir.render_info()
266
+ + f"\nHEAD {short(head) if head else '(base)'} · lab cold",
267
+ "ok": True,
268
+ "handles": [],
269
+ "data": {"summary": ir.summary(), "served_by": "store"},
270
+ }
271
+ scope = args.get("scope") or "L*.*"
272
+ try:
273
+ targets = ir.resolve(scope)
274
+ except WspxError as exc:
275
+ return {"summary": f"ls: {exc}", "ok": False, "handles": [], "data": {}}
276
+ lines = [f"{t.address:<28} {t.param}" for t in targets[: int(args.get("limit", 100))]]
277
+ return {
278
+ "summary": "\n".join(lines) + "\n(lab cold: served from the store)",
279
+ "ok": True,
280
+ "handles": [],
281
+ "data": {"served_by": "store"},
282
+ }
283
+
284
+ def _observe(self, sid: str, name: str, args: dict[str, Any], result: dict[str, Any]) -> str:
285
+ """Repetition across tool calls also counts as a loop (the gateway decides)."""
286
+ if self.gateway is None:
287
+ return ""
288
+ notes = []
289
+ try:
290
+ verdicts = [self.gateway.observe(sid, "call", {"tool": name, "args": args}, label=name)]
291
+ codes = sorted(set(_CODE_RE.findall(result.get("summary") or "")))
292
+ if codes:
293
+ verdicts.append(
294
+ self.gateway.observe(sid, "outcome", ",".join(codes), label=",".join(codes))
295
+ )
296
+ no_op = "E105" in codes
297
+ if no_op:
298
+ verdicts.append(
299
+ self.gateway.observe(sid, "outcome", "no-op edit", label="no-op edit")
300
+ )
301
+ except WspxError:
302
+ return ""
303
+ for verdict in verdicts:
304
+ if verdict is None:
305
+ continue
306
+ self.trace.event(sid, "loop", verdict)
307
+ if verdict["action"] == "stop":
308
+ self.pause(sid, f"loop detected: {verdict['message']}")
309
+ notes.append(f"\n⚠ session paused: {verdict['message']}")
310
+ else:
311
+ notes.append(f"\n⚠ {verdict['message']}")
312
+ return "".join(notes)
313
+
314
+ # ------------------------------------------------------------------ control
315
+
316
+ def touch(self, sid: str) -> None:
317
+ self.last_activity[sid] = time.time()
318
+ self.idle_since.pop(sid, None)
319
+
320
+ def command(self, sid: str, kind: str, text: str | None = None) -> None:
321
+ self.get(sid)
322
+ self.commands[sid].append({"type": kind, **({"text": text} if text else {})})
323
+ self.trace.event(sid, "command", {"type": kind, "text": text}, source="human")
324
+
325
+ def take_commands(self, sid: str) -> list[dict[str, Any]]:
326
+ out = []
327
+ while self.commands[sid]:
328
+ out.append(self.commands[sid].popleft())
329
+ return out
330
+
331
+ def pause(self, sid: str, reason: str) -> None:
332
+ record = self.get(sid)
333
+ record.status, record.reason = "paused", reason
334
+ self._save(record)
335
+ self.command(sid, "pause")
336
+ if self.gateway is not None:
337
+ with contextlib.suppress(WspxError):
338
+ self.gateway.pause(sid, reason)
339
+
340
+ def resume(self, sid: str | None = None) -> None:
341
+ targets = [sid] if sid else [s for s, r in self.records.items() if r.status == "paused"]
342
+ if sid is None:
343
+ self.halted = False
344
+ if self.gateway is not None:
345
+ self.gateway.resume(sid)
346
+ for s in targets:
347
+ record = self.get(s)
348
+ record.status, record.reason = "idle", ""
349
+ self._save(record)
350
+ self.trace.event(s, "session", {"status": "resumed"})
351
+ if record.driver:
352
+ self.start_driver(s)
353
+
354
+ def kill(self, sid: str | None = None) -> int:
355
+ """Stop drivers, revoke leases and terminate labs. ``None`` kills everything and halts."""
356
+ targets = [sid] if sid else list(self.records)
357
+ if sid is None:
358
+ self.halted = True
359
+ if self.gateway is not None:
360
+ with contextlib.suppress(WspxError):
361
+ self.gateway.halt("kill --all")
362
+ for s in targets:
363
+ self.command(s, "stop")
364
+ runner = self.runners.pop(s, None)
365
+ if runner is not None:
366
+ runner.stop()
367
+ self.stop_lab(s, "killed")
368
+ record = self.get(s)
369
+ record.status = "stopped"
370
+ self._save(record)
371
+ return len(targets)
372
+
373
+ def runner_event(self, sid: str, event: dict[str, Any]) -> None:
374
+ self.touch(sid)
375
+ kind = event.get("type")
376
+ if kind == "turn":
377
+ self.trace.turn(sid, event)
378
+ elif kind == "end":
379
+ record = self.get(sid)
380
+ record.status = {
381
+ "done": "done",
382
+ "stopped": "stopped",
383
+ "paused": "paused",
384
+ "refused": "done",
385
+ "halted": "paused",
386
+ "error": "error",
387
+ }.get(event.get("status", ""), "idle")
388
+ record.reason = event.get("reason", "")
389
+ self._save(record)
390
+ self.trace.event(sid, "session", {"status": record.status, "reason": record.reason})
391
+ elif kind != "tool_call": # tool calls are recorded when the control plane runs them
392
+ self.trace.event(sid, str(kind), event, source="driver")
393
+
394
+ # ------------------------------------------------------------------ lifecycle
395
+
396
+ def active(self, sid: str, now: float) -> bool:
397
+ runner = self.runners.get(sid)
398
+ if runner is not None and runner.alive():
399
+ return True
400
+ if self.viewers.get(sid, 0) > 0:
401
+ return True
402
+ return now - self.last_activity.get(sid, 0.0) < RECENT_CALL_S
403
+
404
+ def tick(self, now: float | None = None) -> None:
405
+ """Renew leases of hot and grace labs; release labs whose grace has run out."""
406
+ now = now or time.time()
407
+ for sid in list(self.labs):
408
+ record = self.records[sid]
409
+ if self.active(sid, now) or record.keep_warm == "always":
410
+ self.idle_since.pop(sid, None)
411
+ record.tier = "hot"
412
+ else:
413
+ since = self.idle_since.setdefault(sid, now)
414
+ if now - since >= record.grace_s:
415
+ self.stop_lab(sid, "idle past the grace period")
416
+ continue
417
+ record.tier = "grace"
418
+ try:
419
+ self._persist(sid, self.labs[sid].heartbeat(LEASE_S))
420
+ except Exception as exc:
421
+ self.labs.pop(sid, None)
422
+ record.tier = "cold"
423
+ self.trace.event(sid, "lab", {"tier": "cold", "reason": f"lost: {exc}"})
424
+ self._save(record)
@@ -0,0 +1,183 @@
1
+ """The trace DB (§7): every driver turn and every harness call, joined.
2
+
3
+ ``sessions`` · ``turns`` · ``tool_calls`` · ``artefacts`` · ``events``. The
4
+ events table is the session timeline the UI streams over SSE; every wspx
5
+ result carries a ``call_id`` so external clients' hook payloads join exactly
6
+ to harness calls and the artefacts they produced.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import sqlite3
13
+ import threading
14
+ import time
15
+ from pathlib import Path
16
+ from typing import Any
17
+
18
+ SCHEMA = """
19
+ CREATE TABLE IF NOT EXISTS sessions (id TEXT PRIMARY KEY, data TEXT NOT NULL, updated REAL NOT NULL);
20
+ CREATE TABLE IF NOT EXISTS turns (id INTEGER PRIMARY KEY AUTOINCREMENT, session TEXT NOT NULL, idx INTEGER,
21
+ role TEXT, text TEXT, reasoning TEXT, reasoning_available INTEGER, tokens TEXT, cost REAL, latency REAL,
22
+ ts REAL NOT NULL);
23
+ CREATE TABLE IF NOT EXISTS tool_calls (call_id TEXT PRIMARY KEY, session TEXT NOT NULL, turn INTEGER,
24
+ tool TEXT NOT NULL, args TEXT, summary TEXT, ok INTEGER, latency REAL, error TEXT, source TEXT, ts REAL);
25
+ CREATE TABLE IF NOT EXISTS artefacts (id TEXT NOT NULL, kind TEXT NOT NULL, call_id TEXT, session TEXT,
26
+ ts REAL, PRIMARY KEY (id, call_id));
27
+ CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY AUTOINCREMENT, session TEXT NOT NULL,
28
+ kind TEXT NOT NULL, source TEXT, payload TEXT NOT NULL, ts REAL NOT NULL);
29
+ CREATE INDEX IF NOT EXISTS events_session ON events(session, id);
30
+ CREATE INDEX IF NOT EXISTS calls_session ON tool_calls(session);
31
+ """
32
+
33
+
34
+ def _kind_of(handle: str) -> str:
35
+ if handle.startswith("@r"):
36
+ return "result_set"
37
+ if handle.startswith("dir:"):
38
+ return "direction"
39
+ if handle.startswith("p_"):
40
+ return "patch"
41
+ if handle.startswith("t_"):
42
+ return "trace"
43
+ return "commit"
44
+
45
+
46
+ class TraceDB:
47
+ def __init__(self, path: str | Path) -> None:
48
+ Path(path).parent.mkdir(parents=True, exist_ok=True)
49
+ self._db = sqlite3.connect(
50
+ str(path), check_same_thread=False, isolation_level=None, timeout=30
51
+ )
52
+ self._db.row_factory = sqlite3.Row
53
+ self._db.execute("PRAGMA journal_mode=WAL")
54
+ self._db.executescript(SCHEMA)
55
+ self._lock = threading.Lock()
56
+
57
+ def _exec(self, sql: str, args: tuple[Any, ...] = ()) -> sqlite3.Cursor:
58
+ with self._lock:
59
+ return self._db.execute(sql, args)
60
+
61
+ # sessions
62
+ def put_session(self, record: dict[str, Any]) -> None:
63
+ self._exec(
64
+ "INSERT OR REPLACE INTO sessions VALUES (?,?,?)",
65
+ (record["id"], json.dumps(record), time.time()),
66
+ )
67
+
68
+ def session(self, sid: str) -> dict[str, Any] | None:
69
+ row = self._exec("SELECT data FROM sessions WHERE id = ?", (sid,)).fetchone()
70
+ return json.loads(row["data"]) if row else None
71
+
72
+ def sessions(self) -> list[dict[str, Any]]:
73
+ return [
74
+ json.loads(r["data"])
75
+ for r in self._exec("SELECT data FROM sessions ORDER BY updated DESC")
76
+ ]
77
+
78
+ # timeline
79
+ def event(
80
+ self, session: str, kind: str, payload: dict[str, Any], *, source: str = "wspx"
81
+ ) -> int:
82
+ cur = self._exec(
83
+ "INSERT INTO events (session, kind, source, payload, ts) VALUES (?,?,?,?,?)",
84
+ (session, kind, source, json.dumps(payload, default=str), time.time()),
85
+ )
86
+ return int(cur.lastrowid or 0)
87
+
88
+ def events(self, session: str, since: int = 0, limit: int = 500) -> list[dict[str, Any]]:
89
+ rows = self._exec(
90
+ "SELECT * FROM events WHERE session = ? AND id > ? ORDER BY id LIMIT ?",
91
+ (session, since, limit),
92
+ ).fetchall()
93
+ return [dict(r) | {"payload": json.loads(r["payload"])} for r in rows]
94
+
95
+ def turn(self, session: str, data: dict[str, Any]) -> int:
96
+ self._exec(
97
+ "INSERT INTO turns (session, idx, role, text, reasoning, reasoning_available, tokens, cost, "
98
+ "latency, ts) VALUES (?,?,?,?,?,?,?,?,?,?)",
99
+ (
100
+ session,
101
+ data.get("turn"),
102
+ "assistant",
103
+ data.get("text"),
104
+ data.get("reasoning"),
105
+ int(bool(data.get("reasoning_available"))),
106
+ json.dumps(data.get("usage") or {}),
107
+ data.get("cost"),
108
+ data.get("latency"),
109
+ time.time(),
110
+ ),
111
+ )
112
+ return self.event(session, "turn", data, source=data.get("source", "driver"))
113
+
114
+ def tool_call(
115
+ self,
116
+ session: str,
117
+ call_id: str,
118
+ tool: str,
119
+ args: dict[str, Any],
120
+ result: dict[str, Any],
121
+ *,
122
+ turn: int | None,
123
+ latency: float,
124
+ source: str,
125
+ ) -> int:
126
+ self._exec(
127
+ "INSERT OR REPLACE INTO tool_calls VALUES (?,?,?,?,?,?,?,?,?,?,?)",
128
+ (
129
+ call_id,
130
+ session,
131
+ turn,
132
+ tool,
133
+ json.dumps(args, default=str),
134
+ result.get("summary", "")[:20000],
135
+ int(bool(result.get("ok"))),
136
+ latency,
137
+ None if result.get("ok") else result.get("summary", "")[:500],
138
+ source,
139
+ time.time(),
140
+ ),
141
+ )
142
+ for handle in result.get("handles") or []:
143
+ self._exec(
144
+ "INSERT OR IGNORE INTO artefacts VALUES (?,?,?,?,?)",
145
+ (handle, _kind_of(handle), call_id, session, time.time()),
146
+ )
147
+ commit = (result.get("data") or {}).get("commit")
148
+ if commit:
149
+ self._exec(
150
+ "INSERT OR IGNORE INTO artefacts VALUES (?,?,?,?,?)",
151
+ (commit, "commit", call_id, session, time.time()),
152
+ )
153
+ return self.event(
154
+ session,
155
+ "tool_call",
156
+ {
157
+ "call_id": call_id,
158
+ "tool": tool,
159
+ "args": args,
160
+ "turn": turn,
161
+ "ok": result.get("ok"),
162
+ "summary": result.get("summary", "")[:4000],
163
+ "handles": result.get("handles") or [],
164
+ "latency": latency,
165
+ },
166
+ source=source,
167
+ )
168
+
169
+ def call(self, call_id: str) -> dict[str, Any] | None:
170
+ row = self._exec("SELECT * FROM tool_calls WHERE call_id = ?", (call_id,)).fetchone()
171
+ if row is None:
172
+ return None
173
+ arts = self._exec("SELECT id, kind FROM artefacts WHERE call_id = ?", (call_id,)).fetchall()
174
+ return dict(row) | {
175
+ "args": json.loads(row["args"] or "{}"),
176
+ "artefacts": [dict(a) for a in arts],
177
+ }
178
+
179
+ def calls(self, session: str) -> list[dict[str, Any]]:
180
+ rows = self._exec(
181
+ "SELECT * FROM tool_calls WHERE session = ? ORDER BY ts", (session,)
182
+ ).fetchall()
183
+ return [dict(r) for r in rows]
@@ -0,0 +1 @@
1
+ :root{--bg:#fbfbfa;--panel:#fff;--sunk:#f2f2ef;--line:#e3e2dd;--text:#1d1d1b;--muted:#6d6c66;--accent:#3554d1;--accent-soft:#e8ecfb;--danger:#c0352b;--danger-soft:#fbe9e7;--warn:#9a6400;--ok:#2f7d4f;--attn:#3554d1;--mlp:#2f7d4f;--moe:#8a4fc4;--ssm:#b5651d;--conv:#44708f;--mono:ui-monospace, "SF Mono", Menlo, Consolas, monospace;--lightningcss-light:initial;--lightningcss-dark: ;color-scheme:light}@media (prefers-color-scheme:dark){:root{--bg:#141414;--panel:#1b1b1b;--sunk:#222221;--line:#2f2f2d;--text:#ecebe6;--muted:#9a9990;--accent:#8aa2ff;--accent-soft:#1e2540;--danger:#ff7b70;--danger-soft:#3a1d1a;--warn:#e6b450;--ok:#6cc591;--attn:#8aa2ff;--mlp:#6cc591;--moe:#c39bf0;--ssm:#e8a265;--conv:#86b3d1;--lightningcss-light: ;--lightningcss-dark:initial;color-scheme:dark}}*{box-sizing:border-box}body{background:var(--bg);color:var(--text);margin:0;font:14px/1.45 system-ui,-apple-system,Segoe UI,sans-serif}code,pre{font-family:var(--mono);font-size:12.5px}pre{white-space:pre-wrap;word-break:break-word;margin:0}button{font:inherit;border:1px solid var(--line);background:var(--panel);color:var(--text);cursor:pointer;border-radius:6px;padding:6px 12px}button:hover:not(:disabled){border-color:var(--accent)}button:disabled{opacity:.5;cursor:default}button.danger{color:var(--danger);border-color:var(--danger)}button.ghost{background:0 0}button.link{color:inherit;background:0 0;border:0;padding:0;text-decoration:underline}button.wide{width:100%}input,textarea{font:inherit;border:1px solid var(--line);background:var(--panel);width:100%;color:var(--text);border-radius:6px;padding:6px 8px}a{color:var(--accent)}.muted{color:var(--muted)}.small{font-size:12px}.spacer{flex:1}.row{align-items:center;gap:8px;display:flex}.error{color:var(--danger);background:var(--danger-soft);border-radius:6px;margin-bottom:12px;padding:8px 12px}.badge{background:var(--sunk);border:1px solid var(--line);border-radius:999px;margin-left:6px;padding:1px 8px;font-size:12px;display:inline-block}.badge.danger{color:var(--danger);border-color:var(--danger)}.badge.warn{color:var(--warn);border-color:var(--warn)}.badge.tier-hot{color:var(--ok);border-color:var(--ok)}.badge.tier-grace{color:var(--warn);border-color:var(--warn)}.app{flex-direction:column;height:100vh;display:flex}.topbar{border-bottom:1px solid var(--line);background:var(--panel);align-items:center;gap:12px;padding:10px 16px;display:flex}.brand{letter-spacing:.02em;font-weight:700}.body{flex:1;min-height:0;display:flex}.sidebar{border-right:1px solid var(--line);background:var(--panel);flex-shrink:0;width:270px;padding:12px;overflow:auto}.sidebar h3{text-transform:uppercase;letter-spacing:.06em;color:var(--muted);margin:16px 0 6px;font-size:12px}.sessions{margin:0;padding:0;list-style:none}.sessions li{cursor:pointer;border:1px solid #0000;border-radius:6px;padding:8px}.sessions li:hover{background:var(--sunk)}.sessions li.active{background:var(--accent-soft);border-color:var(--accent)}.sessions .row{justify-content:space-between}.dot{background:var(--muted);border-radius:50%;width:8px;height:8px}.dot.hot{background:var(--ok)}.dot.grace{background:var(--warn)}.status.running{color:var(--ok)}.status.paused,.status.error{color:var(--danger)}.new-session{flex-direction:column;gap:8px;display:flex}.new-session label{color:var(--muted);flex-direction:column;gap:2px;font-size:12px;display:flex}.main{flex-direction:column;flex:1;min-width:0;padding:12px 20px;display:flex;overflow:hidden}.empty{color:var(--muted);margin:auto}.tabs{border-bottom:1px solid var(--line);flex-shrink:0;gap:4px;margin-bottom:12px;display:flex;overflow-x:auto}.tab{white-space:nowrap;color:var(--muted);background:0 0;border:0;border-bottom:2px solid #0000;border-radius:0}.tab.active{color:var(--text);border-bottom-color:var(--accent)}.panel{flex:1;overflow:auto}.login{flex-direction:column;gap:12px;max-width:360px;margin:15vh auto;display:flex}.login form{gap:8px;display:flex}.session-view{flex-direction:column;gap:10px;display:flex}.controls{align-items:center;gap:8px;display:flex}.steer{gap:8px;display:flex}.timeline{flex-direction:column;gap:8px;margin:0;padding:0;list-style:none;display:flex}.ev{border:1px solid var(--line);background:var(--panel);border-radius:8px;padding:8px 12px}.ev .meta{color:var(--muted);margin-bottom:4px;font-size:12px}.ev.turn{border-left:3px solid var(--accent)}.ev.tool{border-left:3px solid var(--ok)}.ev.tool.failed{border-left-color:var(--danger)}.ev.note{color:var(--muted);border-style:dashed;font-size:13px}.ev.loop{color:var(--warn);border-color:var(--warn)}.ev .text{white-space:pre-wrap}.ev .call{font-size:12.5px}.ev .args{color:var(--muted)}.reasoning{color:var(--muted);background:var(--sunk);border-radius:6px;padding:8px}.result{background:var(--sunk);border-radius:6px;max-height:360px;padding:8px 10px;overflow:auto}.handle{color:var(--accent);margin-left:4px}.arch-layout{grid-template-columns:minmax(0,1fr) 400px;gap:16px;display:grid}.blocks{flex-direction:column;gap:6px;display:flex}.stream-cap{border:1px dashed var(--line);border-radius:6px;flex-wrap:wrap;gap:4px;padding:6px;display:flex}.block{border:1px solid var(--line);background:var(--panel);border-radius:8px;padding:6px 8px}.block-head{margin-bottom:4px}.branch{border-left:3px solid var(--muted);flex-wrap:wrap;align-items:center;gap:4px;margin:2px 0;padding:3px 0 3px 8px;display:flex}.branch.attn{border-left-color:var(--attn)}.branch.mlp{border-left-color:var(--mlp)}.branch.moe{border-left-color:var(--moe)}.branch.ssm{border-left-color:var(--ssm)}.branch.conv{border-left-color:var(--conv)}.branch-name{color:var(--muted);width:72px;font-size:12px}.chip{font-size:12px;font-family:var(--mono);border-radius:999px;padding:1px 8px}.chip.writer{border-color:var(--ok)}.chip.reader{border-color:var(--accent)}.chip.active{background:var(--accent-soft)}.inspector .result{white-space:pre;overflow-x:auto}.inspector{border:1px solid var(--line);background:var(--panel);border-radius:8px;align-self:start;padding:12px;position:sticky;top:0}.inspector dl,.lab dl{grid-template-columns:100px 1fr;gap:4px 12px;display:grid}.inspector dt,.lab dt{color:var(--muted)}.inspector dd,.lab dd{overflow-wrap:anywhere;margin:0}.commit-list{margin:12px 0;padding:0;list-style:none}.commit-list li{cursor:pointer;border-radius:6px;flex-wrap:wrap;align-items:baseline;gap:8px;padding:6px 8px;display:flex}.commit-list li:hover{background:var(--sunk)}.commit-list li.picked{background:var(--accent-soft)}.commit-list .message{flex:1;min-width:200px}.graph{border:2px solid var(--accent);border-radius:50%;flex-shrink:0;width:10px;height:10px}.graph.merge{background:var(--accent)}table{border-collapse:collapse;width:100%}th,td{text-align:left;border-bottom:1px solid var(--line);vertical-align:top;padding:6px 8px}th{color:var(--muted);font-size:12px;font-weight:500}.matrix .cell{cursor:pointer;background:linear-gradient(90deg, var(--accent-soft) var(--pct), transparent var(--pct))}.items{margin-top:16px}tr.failed td{color:var(--danger)}.traces{grid-template-columns:260px minmax(0,1fr);gap:16px;display:grid}.trace-list{margin:0;padding:0;list-style:none}.trace-list li{cursor:pointer;border-radius:6px;padding:6px 8px}.trace-list li.active,.trace-list li:hover{background:var(--accent-soft)}.trace{flex-direction:column;gap:12px;display:flex}.bar{background:var(--accent);vertical-align:middle;border-radius:4px;min-width:2px;height:8px;display:inline-block}.token{font-family:var(--mono);margin-right:10px;font-size:12.5px;display:inline-block}.div-row{grid-template-columns:40px 1fr 80px;align-items:center;gap:8px;display:grid}.ranking{padding-left:20px}.ranking .bar{background:var(--warn);max-width:40%}@media (width<=720px){.body{flex-direction:column}.sidebar{border-right:0;border-bottom:1px solid var(--line);width:100%;max-height:35vh}.main{padding:12px 16px}.arch-layout,.traces{grid-template-columns:1fr}.inspector{position:static}}