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/gateway/ledger.py ADDED
@@ -0,0 +1,536 @@
1
+ """The spend ledger (§7): SQLite on its own volume, written only by the gateway.
2
+
3
+ Tables: ``limits`` (user-set, empty by default), ``reservations`` (open
4
+ worst-case holds, used only while a dollar limit is set), ``usage`` (settled
5
+ cost per call), ``loop_events``, ``halt``, plus per-session state, pending
6
+ nudges, price overrides and limit notices.
7
+
8
+ Dollar limits never overshoot. Before a call the gateway reserves its
9
+ worst-case cost against every applicable limit inside one ``BEGIN
10
+ IMMEDIATE`` transaction; afterwards the hold settles to the actual cost. A
11
+ call that ends without usage (a broken stream) keeps its full reservation.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import json
17
+ import secrets
18
+ import sqlite3
19
+ import threading
20
+ import time
21
+ from collections.abc import Iterator
22
+ from contextlib import contextmanager
23
+ from dataclasses import dataclass
24
+ from datetime import UTC, datetime
25
+ from pathlib import Path
26
+ from typing import Any, Literal
27
+
28
+ from pydantic import BaseModel, Field, model_validator
29
+
30
+ from wspx.gateway.prices import Price, PriceTable
31
+
32
+ Scope = Literal["call", "session", "day", "month", "rate", "velocity"]
33
+ Metric = Literal[
34
+ "input_tokens",
35
+ "output_tokens",
36
+ "dollars",
37
+ "calls",
38
+ "turns",
39
+ "tool_calls",
40
+ "wall_clock_s",
41
+ "gpu_minutes",
42
+ "gpu_dollars",
43
+ "concurrent_sessions",
44
+ ]
45
+ GATEWAY_METRICS = frozenset({"input_tokens", "output_tokens", "dollars", "calls", "turns"})
46
+ NOTICE_LEVELS = (0.5, 0.8, 1.0)
47
+
48
+ SCHEMA = """
49
+ CREATE TABLE IF NOT EXISTS limits (
50
+ id TEXT PRIMARY KEY, scope TEXT NOT NULL, metric TEXT NOT NULL, value REAL NOT NULL,
51
+ session TEXT, provider TEXT, window_s REAL, created REAL NOT NULL);
52
+ CREATE TABLE IF NOT EXISTS reservations (
53
+ id TEXT PRIMARY KEY, session TEXT NOT NULL, provider TEXT NOT NULL, model TEXT NOT NULL,
54
+ amount REAL NOT NULL, created REAL NOT NULL);
55
+ CREATE TABLE IF NOT EXISTS usage (
56
+ id INTEGER PRIMARY KEY AUTOINCREMENT, ts REAL NOT NULL, session TEXT NOT NULL, provider TEXT NOT NULL,
57
+ model TEXT NOT NULL, input INTEGER, output INTEGER, cache_read INTEGER, cache_write INTEGER,
58
+ cost REAL, charged REAL NOT NULL DEFAULT 0, status INTEGER, error TEXT);
59
+ CREATE INDEX IF NOT EXISTS usage_session ON usage(session);
60
+ CREATE INDEX IF NOT EXISTS usage_ts ON usage(ts);
61
+ CREATE TABLE IF NOT EXISTS loop_events (
62
+ id INTEGER PRIMARY KEY AUTOINCREMENT, ts REAL NOT NULL, session TEXT NOT NULL, action TEXT NOT NULL,
63
+ kind TEXT NOT NULL, key TEXT NOT NULL, evidence TEXT NOT NULL, message TEXT NOT NULL);
64
+ CREATE TABLE IF NOT EXISTS halt (id INTEGER PRIMARY KEY CHECK (id = 1), active INTEGER NOT NULL,
65
+ reason TEXT, ts REAL);
66
+ CREATE TABLE IF NOT EXISTS sessions (session TEXT PRIMARY KEY, paused INTEGER NOT NULL DEFAULT 0,
67
+ reason TEXT, ts REAL);
68
+ CREATE TABLE IF NOT EXISTS nudges (id INTEGER PRIMARY KEY AUTOINCREMENT, ts REAL NOT NULL,
69
+ session TEXT NOT NULL, message TEXT NOT NULL, delivered INTEGER NOT NULL DEFAULT 0);
70
+ CREATE TABLE IF NOT EXISTS prices (key TEXT PRIMARY KEY, input REAL NOT NULL, output REAL NOT NULL,
71
+ cache_read REAL, cache_write REAL);
72
+ CREATE TABLE IF NOT EXISTS notices (id INTEGER PRIMARY KEY AUTOINCREMENT, ts REAL NOT NULL,
73
+ limit_id TEXT NOT NULL, bucket TEXT NOT NULL, level REAL NOT NULL, value REAL NOT NULL,
74
+ UNIQUE (limit_id, bucket, level));
75
+ """
76
+
77
+
78
+ class Limit(BaseModel):
79
+ """A user-set limit. None exist by default."""
80
+
81
+ id: str = Field(default_factory=lambda: "lim_" + secrets.token_hex(4))
82
+ scope: Scope
83
+ metric: Metric
84
+ value: float = Field(gt=0)
85
+ session: str | None = None
86
+ """For session-scoped limits: one session, or every session when unset."""
87
+ provider: str | None = None
88
+ window_s: float | None = Field(default=None, gt=0)
89
+ """Rate and velocity window (rate defaults to 60 s)."""
90
+
91
+ @model_validator(mode="after")
92
+ def _shape(self) -> Limit:
93
+ allowed = {
94
+ "call": {"input_tokens", "output_tokens"},
95
+ "session": {
96
+ "dollars",
97
+ "calls",
98
+ "turns",
99
+ "tool_calls",
100
+ "wall_clock_s",
101
+ "gpu_minutes",
102
+ "gpu_dollars",
103
+ },
104
+ "day": {"dollars"},
105
+ "month": {"dollars"},
106
+ "rate": {"calls", "concurrent_sessions"},
107
+ "velocity": {"dollars"},
108
+ }[self.scope]
109
+ if self.metric not in allowed:
110
+ raise ValueError(
111
+ f"{self.scope} limits take {', '.join(sorted(allowed))}, not {self.metric}"
112
+ )
113
+ if self.scope == "velocity" and self.window_s is None:
114
+ raise ValueError("a velocity limit needs window_s, e.g. 600 for 'per 10 minutes'")
115
+ return self
116
+
117
+ def describe(self) -> str:
118
+ where = f" for {self.session}" if self.session else ""
119
+ prov = f" on {self.provider}" if self.provider else ""
120
+ window = f" per {self.window_s:g}s" if self.window_s else ""
121
+ return f"{self.metric} ≤ {self.value:g} per {self.scope}{window}{where}{prov}"
122
+
123
+
124
+ @dataclass
125
+ class Refusal:
126
+ limit: Limit | None
127
+ message: str
128
+
129
+
130
+ class Ledger:
131
+ def __init__(self, path: str | Path) -> None:
132
+ self.path = Path(path)
133
+ self.path.parent.mkdir(parents=True, exist_ok=True)
134
+ self._db = sqlite3.connect(
135
+ str(self.path), isolation_level=None, check_same_thread=False, timeout=30
136
+ )
137
+ self._db.row_factory = sqlite3.Row
138
+ self._db.execute("PRAGMA journal_mode=WAL")
139
+ self._db.executescript(SCHEMA)
140
+ self._lock = threading.RLock()
141
+
142
+ @contextmanager
143
+ def tx(self) -> Iterator[sqlite3.Connection]:
144
+ with self._lock:
145
+ self._db.execute("BEGIN IMMEDIATE")
146
+ try:
147
+ yield self._db
148
+ except BaseException:
149
+ self._db.execute("ROLLBACK")
150
+ raise
151
+ self._db.execute("COMMIT")
152
+
153
+ def close(self) -> None:
154
+ self._db.close()
155
+
156
+ # ------------------------------------------------------------------ limits
157
+
158
+ def limits(self) -> list[Limit]:
159
+ with self._lock:
160
+ rows = self._db.execute("SELECT * FROM limits ORDER BY created").fetchall()
161
+ return [
162
+ Limit(
163
+ **{
164
+ k: r[k]
165
+ for k in ("id", "scope", "metric", "value", "session", "provider", "window_s")
166
+ }
167
+ )
168
+ for r in rows
169
+ ]
170
+
171
+ def set_limit(self, limit: Limit) -> Limit:
172
+ with self.tx() as db:
173
+ db.execute(
174
+ "INSERT OR REPLACE INTO limits VALUES (?,?,?,?,?,?,?,?)",
175
+ (
176
+ limit.id,
177
+ limit.scope,
178
+ limit.metric,
179
+ limit.value,
180
+ limit.session,
181
+ limit.provider,
182
+ limit.window_s,
183
+ time.time(),
184
+ ),
185
+ )
186
+ return limit
187
+
188
+ def remove_limit(self, limit_id: str) -> bool:
189
+ with self.tx() as db:
190
+ return db.execute("DELETE FROM limits WHERE id = ?", (limit_id,)).rowcount > 0
191
+
192
+ # ------------------------------------------------------------------ prices
193
+
194
+ def price_table(self) -> PriceTable:
195
+ with self._lock:
196
+ rows = self._db.execute("SELECT * FROM prices").fetchall()
197
+ return PriceTable(
198
+ {
199
+ r["key"]: Price(r["input"], r["output"], r["cache_read"], r["cache_write"])
200
+ for r in rows
201
+ }
202
+ )
203
+
204
+ def set_price(self, provider: str, model: str, price: Price) -> None:
205
+ with self.tx() as db:
206
+ db.execute(
207
+ "INSERT OR REPLACE INTO prices VALUES (?,?,?,?,?)",
208
+ (
209
+ f"{provider}:{model}",
210
+ price.input,
211
+ price.output,
212
+ price.cache_read,
213
+ price.cache_write,
214
+ ),
215
+ )
216
+
217
+ # ------------------------------------------------------------------ halt / pause
218
+
219
+ def halted(self) -> str | None:
220
+ with self._lock:
221
+ row = self._db.execute("SELECT active, reason FROM halt WHERE id = 1").fetchone()
222
+ return (row["reason"] or "halted") if row and row["active"] else None
223
+
224
+ def set_halt(self, active: bool, reason: str = "") -> None:
225
+ with self.tx() as db:
226
+ db.execute(
227
+ "INSERT OR REPLACE INTO halt VALUES (1, ?, ?, ?)",
228
+ (int(active), reason, time.time()),
229
+ )
230
+
231
+ def paused(self, session: str) -> str | None:
232
+ with self._lock:
233
+ row = self._db.execute(
234
+ "SELECT paused, reason FROM sessions WHERE session = ?", (session,)
235
+ ).fetchone()
236
+ return (row["reason"] or "paused") if row and row["paused"] else None
237
+
238
+ def pause(self, session: str, reason: str) -> None:
239
+ with self.tx() as db:
240
+ db.execute(
241
+ "INSERT OR REPLACE INTO sessions VALUES (?, 1, ?, ?)",
242
+ (session, reason, time.time()),
243
+ )
244
+
245
+ def resume(self, session: str | None = None) -> None:
246
+ with self.tx() as db:
247
+ if session is None:
248
+ db.execute("UPDATE sessions SET paused = 0")
249
+ db.execute("INSERT OR REPLACE INTO halt VALUES (1, 0, '', ?)", (time.time(),))
250
+ else:
251
+ db.execute("UPDATE sessions SET paused = 0 WHERE session = ?", (session,))
252
+
253
+ # ------------------------------------------------------------------ nudges and loop events
254
+
255
+ def add_nudge(self, session: str, message: str) -> None:
256
+ with self.tx() as db:
257
+ db.execute(
258
+ "INSERT INTO nudges (ts, session, message) VALUES (?,?,?)",
259
+ (time.time(), session, message),
260
+ )
261
+
262
+ def take_nudges(self, session: str) -> list[str]:
263
+ with self.tx() as db:
264
+ rows = db.execute(
265
+ "SELECT id, message FROM nudges WHERE session = ? AND delivered = 0 ORDER BY id",
266
+ (session,),
267
+ ).fetchall()
268
+ db.execute(
269
+ "UPDATE nudges SET delivered = 1 WHERE session = ? AND delivered = 0", (session,)
270
+ )
271
+ return [r["message"] for r in rows]
272
+
273
+ def loop_event(self, session: str, verdict: dict[str, Any]) -> None:
274
+ with self.tx() as db:
275
+ db.execute(
276
+ "INSERT INTO loop_events (ts, session, action, kind, key, evidence, message) "
277
+ "VALUES (?,?,?,?,?,?,?)",
278
+ (
279
+ time.time(),
280
+ session,
281
+ verdict["action"],
282
+ verdict["kind"],
283
+ verdict["key"],
284
+ json.dumps(verdict["evidence"]),
285
+ verdict["message"],
286
+ ),
287
+ )
288
+
289
+ def loop_events(self, session: str | None = None) -> list[dict[str, Any]]:
290
+ with self._lock:
291
+ sql = (
292
+ "SELECT * FROM loop_events"
293
+ + (" WHERE session = ?" if session else "")
294
+ + " ORDER BY id"
295
+ )
296
+ rows = self._db.execute(sql, (session,) if session else ()).fetchall()
297
+ return [dict(r) | {"evidence": json.loads(r["evidence"])} for r in rows]
298
+
299
+ # ------------------------------------------------------------------ reserve / settle
300
+
301
+ def reserve(
302
+ self,
303
+ session: str,
304
+ provider: str,
305
+ model: str,
306
+ *,
307
+ input_bound: int,
308
+ max_output: int,
309
+ price: Price | None,
310
+ now: float | None = None,
311
+ ) -> tuple[str | None, Refusal | None]:
312
+ """Check every applicable limit; hold the worst-case cost if a dollar limit applies."""
313
+ now = now or time.time()
314
+ with self.tx() as db:
315
+ limits = [
316
+ lim
317
+ for lim in self._load(db)
318
+ if lim.metric in GATEWAY_METRICS
319
+ and (lim.provider is None or lim.provider == provider)
320
+ and (lim.session is None or lim.session == session)
321
+ ]
322
+ for lim in limits:
323
+ if lim.scope == "call" and lim.metric == "input_tokens" and input_bound > lim.value:
324
+ return None, Refusal(
325
+ lim,
326
+ f"this call may send up to {input_bound:,} input tokens; "
327
+ f"limit {lim.describe()}",
328
+ )
329
+ if lim.scope == "call" and lim.metric == "output_tokens" and max_output > lim.value:
330
+ return None, Refusal(
331
+ lim, f"max output {max_output:,} tokens exceeds {lim.describe()}"
332
+ )
333
+ if lim.metric in ("calls", "turns") and lim.scope in ("session", "rate"):
334
+ since = now - (lim.window_s or 60) if lim.scope == "rate" else 0.0
335
+ where, args = self._where(lim, session, provider, since)
336
+ used = db.execute(f"SELECT COUNT(*) FROM usage WHERE {where}", args).fetchone()[
337
+ 0
338
+ ]
339
+ held = db.execute(
340
+ f"SELECT COUNT(*) FROM reservations WHERE {where.replace('ts', 'created')}",
341
+ args,
342
+ ).fetchone()[0]
343
+ if used + held + 1 > lim.value:
344
+ return None, Refusal(
345
+ lim, f"{lim.describe()} reached ({used + held:g} used)"
346
+ )
347
+ dollar = [lim for lim in limits if lim.metric == "dollars"]
348
+ if not dollar:
349
+ return None, None
350
+ if price is None:
351
+ return None, Refusal(
352
+ dollar[0],
353
+ f"price of {provider}:{model} is unknown, so the dollar limit "
354
+ f"{dollar[0].describe()} cannot be enforced; set it with "
355
+ f"`wspx limits price {provider}:{model} <input> <output>`",
356
+ )
357
+ worst = (input_bound * max(price.input, price.write) + max_output * price.output) / 1e6
358
+ for lim in dollar:
359
+ spent = self._spent(db, lim, session, provider, now)
360
+ if spent + worst > lim.value + 1e-12:
361
+ return None, Refusal(
362
+ lim,
363
+ f"{lim.describe()}: ${spent:.4f} spent or held, this call may cost "
364
+ f"up to ${worst:.4f}",
365
+ )
366
+ rid = "res_" + secrets.token_hex(6)
367
+ db.execute(
368
+ "INSERT INTO reservations VALUES (?,?,?,?,?,?)",
369
+ (rid, session, provider, model, worst, now),
370
+ )
371
+ return rid, None
372
+
373
+ def settle(
374
+ self,
375
+ reservation: str | None,
376
+ session: str,
377
+ provider: str,
378
+ model: str,
379
+ *,
380
+ input: int = 0,
381
+ output: int = 0,
382
+ cache_read: int = 0,
383
+ cache_write: int = 0,
384
+ cost: float | None,
385
+ status: int = 200,
386
+ error: str | None = None,
387
+ now: float | None = None,
388
+ ) -> list[dict[str, Any]]:
389
+ """Record a finished call. Returns any limit notices it triggered (50/80/100%)."""
390
+ now = now or time.time()
391
+ with self.tx() as db:
392
+ held = 0.0
393
+ if reservation is not None:
394
+ row = db.execute(
395
+ "SELECT amount FROM reservations WHERE id = ?", (reservation,)
396
+ ).fetchone()
397
+ held = float(row["amount"]) if row else 0.0
398
+ db.execute("DELETE FROM reservations WHERE id = ?", (reservation,))
399
+ charged = cost if cost is not None else held # no usage: keep the whole hold
400
+ db.execute(
401
+ "INSERT INTO usage (ts, session, provider, model, input, output, cache_read, cache_write, "
402
+ "cost, charged, status, error) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",
403
+ (
404
+ now,
405
+ session,
406
+ provider,
407
+ model,
408
+ input,
409
+ output,
410
+ cache_read,
411
+ cache_write,
412
+ cost,
413
+ charged,
414
+ status,
415
+ error,
416
+ ),
417
+ )
418
+ return self._notices(db, session, provider, now)
419
+
420
+ def _load(self, db: sqlite3.Connection) -> list[Limit]:
421
+ rows = db.execute("SELECT * FROM limits").fetchall()
422
+ return [
423
+ Limit(
424
+ **{
425
+ k: r[k]
426
+ for k in ("id", "scope", "metric", "value", "session", "provider", "window_s")
427
+ }
428
+ )
429
+ for r in rows
430
+ ]
431
+
432
+ @staticmethod
433
+ def _bucket_start(scope: str, now: float, window: float | None) -> float:
434
+ if scope == "day":
435
+ d = datetime.fromtimestamp(now, UTC)
436
+ return datetime(d.year, d.month, d.day, tzinfo=UTC).timestamp()
437
+ if scope == "month":
438
+ d = datetime.fromtimestamp(now, UTC)
439
+ return datetime(d.year, d.month, 1, tzinfo=UTC).timestamp()
440
+ if scope in ("velocity", "rate"):
441
+ return now - (window or 60)
442
+ return 0.0
443
+
444
+ def _where(
445
+ self, lim: Limit, session: str, provider: str, since: float
446
+ ) -> tuple[str, tuple[Any, ...]]:
447
+ """Rows a limit counts: session limits see their session; the others are global
448
+ unless the limit names a session or a provider."""
449
+ clauses: list[str] = ["ts >= ?"]
450
+ args: list[Any] = [since]
451
+ if lim.scope == "session" or lim.session:
452
+ clauses.append("session = ?")
453
+ args.append(session)
454
+ if lim.provider:
455
+ clauses.append("provider = ?")
456
+ args.append(provider)
457
+ return " AND ".join(clauses), tuple(args)
458
+
459
+ def _spent(
460
+ self, db: sqlite3.Connection, lim: Limit, session: str, provider: str, now: float
461
+ ) -> float:
462
+ since = self._bucket_start(lim.scope, now, lim.window_s)
463
+ where, args = self._where(lim, session, provider, since)
464
+ used = db.execute(
465
+ f"SELECT COALESCE(SUM(charged), 0) FROM usage WHERE {where}", args
466
+ ).fetchone()[0]
467
+ held = db.execute(
468
+ f"SELECT COALESCE(SUM(amount), 0) FROM reservations WHERE {where.replace('ts', 'created')}",
469
+ args,
470
+ ).fetchone()[0]
471
+ return float(used) + float(held)
472
+
473
+ def _notices(
474
+ self, db: sqlite3.Connection, session: str, provider: str, now: float
475
+ ) -> list[dict[str, Any]]:
476
+ out = []
477
+ for lim in self._load(db):
478
+ if lim.metric != "dollars" or (lim.provider and lim.provider != provider):
479
+ continue
480
+ if lim.session and lim.session != session:
481
+ continue
482
+ since = self._bucket_start(lim.scope, now, lim.window_s)
483
+ where, args = self._where(lim, session, provider, since)
484
+ spent = float(
485
+ db.execute(
486
+ f"SELECT COALESCE(SUM(charged), 0) FROM usage WHERE {where}", args
487
+ ).fetchone()[0]
488
+ )
489
+ bucket = f"{lim.scope}:{session if lim.scope == 'session' else int(since)}"
490
+ for level in NOTICE_LEVELS:
491
+ if spent >= level * lim.value:
492
+ cur = db.execute(
493
+ "INSERT OR IGNORE INTO notices (ts, limit_id, bucket, level, value) "
494
+ "VALUES (?,?,?,?,?)",
495
+ (now, lim.id, bucket, level, spent),
496
+ )
497
+ if cur.rowcount:
498
+ out.append(
499
+ {
500
+ "limit": lim.id,
501
+ "describe": lim.describe(),
502
+ "level": level,
503
+ "spent": spent,
504
+ }
505
+ )
506
+ return out
507
+
508
+ # ------------------------------------------------------------------ reporting
509
+
510
+ def usage_summary(self, *, session: str | None = None, since: float = 0.0) -> dict[str, Any]:
511
+ with self._lock:
512
+ where = "ts >= ?" + (" AND session = ?" if session else "")
513
+ args: tuple[Any, ...] = (since, session) if session else (since,)
514
+ rows = self._db.execute(
515
+ f"SELECT session, provider, model, COUNT(*) n, SUM(input) i, SUM(output) o, SUM(cache_read) cr, "
516
+ f"SUM(cache_write) cw, SUM(cost) c, SUM(charged) ch, SUM(cost IS NULL) unknown "
517
+ f"FROM usage WHERE {where} GROUP BY session, provider, model",
518
+ args,
519
+ ).fetchall()
520
+ held = self._db.execute("SELECT COALESCE(SUM(amount), 0) FROM reservations").fetchone()[
521
+ 0
522
+ ]
523
+ groups = [dict(r) for r in rows]
524
+ return {
525
+ "groups": groups,
526
+ "total_charged": sum(g["ch"] or 0 for g in groups),
527
+ "unknown_cost_calls": sum(g["unknown"] or 0 for g in groups),
528
+ "held": held,
529
+ }
530
+
531
+ def notices(self, since: float = 0.0) -> list[dict[str, Any]]:
532
+ with self._lock:
533
+ rows = self._db.execute(
534
+ "SELECT * FROM notices WHERE ts >= ? ORDER BY id", (since,)
535
+ ).fetchall()
536
+ return [dict(r) for r in rows]
wspx/gateway/prices.py ADDED
@@ -0,0 +1,75 @@
1
+ """Model prices, in dollars per million tokens.
2
+
3
+ Prices ship with each release and can be overridden at runtime. A model with
4
+ no known price still works: its cost shows as unknown. Only a dollar limit
5
+ needs a price, and then wspx asks for it rather than guessing.
6
+
7
+ Anthropic prices are first-party API rates checked on 2026-11-03. Cache
8
+ writes are 1.25× input (5-minute TTL); cache reads are 0.1× input unless a
9
+ model's list price says otherwise.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from dataclasses import dataclass
15
+
16
+
17
+ @dataclass(frozen=True)
18
+ class Price:
19
+ input: float
20
+ output: float
21
+ cache_read: float | None = None
22
+ cache_write: float | None = None
23
+
24
+ @property
25
+ def read(self) -> float:
26
+ return self.cache_read if self.cache_read is not None else self.input * 0.1
27
+
28
+ @property
29
+ def write(self) -> float:
30
+ return self.cache_write if self.cache_write is not None else self.input * 1.25
31
+
32
+
33
+ def _a(inp: float, out: float, read: float | None = None) -> Price:
34
+ return Price(inp, out, read)
35
+
36
+
37
+ BUILTIN: dict[str, Price] = {
38
+ "anthropic:claude-fable-5-2": _a(8.0, 40.0, 0.20),
39
+ "anthropic:claude-mythos-5-2": _a(8.0, 40.0, 0.20),
40
+ "anthropic:claude-fable-5-1": _a(10.0, 50.0),
41
+ "anthropic:claude-mythos-5-1": _a(10.0, 50.0),
42
+ "anthropic:claude-fable-5": _a(10.0, 50.0),
43
+ "anthropic:claude-opus-5-5": _a(4.0, 20.0, 0.20),
44
+ "anthropic:claude-opus-5": _a(5.0, 25.0),
45
+ "anthropic:claude-opus-4-8": _a(5.0, 25.0),
46
+ "anthropic:claude-opus-4-7": _a(5.0, 25.0),
47
+ "anthropic:claude-opus-4-6": _a(5.0, 25.0),
48
+ "anthropic:claude-sonnet-5-5": _a(2.0, 10.0, 0.10),
49
+ "anthropic:claude-sonnet-5": _a(2.0, 10.0),
50
+ "anthropic:claude-sonnet-4-6": _a(3.0, 15.0),
51
+ "anthropic:claude-haiku-5-5": _a(0.10, 0.50),
52
+ "anthropic:claude-haiku-4-5": _a(1.0, 5.0),
53
+ }
54
+
55
+
56
+ class PriceTable:
57
+ def __init__(self, overrides: dict[str, Price] | None = None) -> None:
58
+ self.overrides = dict(overrides or {})
59
+
60
+ def get(self, provider: str, model: str) -> Price | None:
61
+ key = f"{provider}:{model}"
62
+ if key in self.overrides:
63
+ return self.overrides[key]
64
+ if key in BUILTIN:
65
+ return BUILTIN[key]
66
+ # dated snapshots and fast variants map onto their family's list price
67
+ for candidate in (key.rsplit("-", 1)[0], key.removesuffix("-fast")):
68
+ if candidate in self.overrides:
69
+ return self.overrides[candidate]
70
+ if candidate in BUILTIN:
71
+ return BUILTIN[candidate]
72
+ return None
73
+
74
+ def set(self, provider: str, model: str, price: Price) -> None:
75
+ self.overrides[f"{provider}:{model}"] = price