spend: ONE all-time figure, computed once, for every surface that shows it
ci/woodpecker/push/woodpecker Pipeline was canceled
ci/woodpecker/push/woodpecker Pipeline was canceled
The panel's card said $941.37 and the member dashboard's said $900.51 for the
same claim. The panel used its own per-team query; the dashboard summed the
chart series IN THE BROWSER, which disagrees with the server whenever a row is
null, capped or stale.
coop_spend_totals() is now the definition: one GROUP BY over SpendLogs joined to
the team table, so
spend_all_time = every row (members, operators, agent teams, sandboxes) —
the same universe litellm_spend_by_day() sums, so the
cards and the money chart agree by construction
own_all_time_spend = the part that is the co-op's own (non-member teams, plus
rows with no team)
and member_all_time_spend = the rest. Exposed through /admin/overview (the
panel's boot payload), the money payload, and /broker/metrics (the dashboard's).
_coop_spend_totals_safe() returns {} rather than a zero: a failed computation
must never read as "the co-op has spent nothing".
tests/test_coop_spend_totals.py pins the arithmetic (total == member + own ==
every row) and the no-fake-zero rule.
This commit is contained in:
1 parent
e15a01c338
commit
2a77b0c988
2 files changed
+180
-1
No files matched your search
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Regression test: ONE definition of the co-op's all-time spend.
|
||||
|
||||
WHY THIS EXISTS
|
||||
On 2026-11-13 the panel's card said $941.37 and the member dashboard's said
|
||||
$900.51 — the same claim, two numbers. The panel used the portal's per-team
|
||||
query; the dashboard summed the chart series IN THE BROWSER, which disagrees
|
||||
with the server whenever a row is null, capped or stale. coop_spend_totals()
|
||||
is now the single source, exposed to the panel's boot payload, the money
|
||||
payload and the broker's metrics.
|
||||
|
||||
The split has to be the same split on every surface, and a failure to compute
|
||||
it must NEVER look like "$0 spent": the safe wrapper returns {} so callers
|
||||
omit the key instead of printing a zero that reads as fact.
|
||||
"""
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
import app.main as m # noqa: E402
|
||||
|
||||
FAILS = []
|
||||
|
||||
|
||||
def check(cond, msg):
|
||||
print((" ok " if cond else " FAIL ") + msg)
|
||||
if not cond:
|
||||
FAILS.append(msg)
|
||||
|
||||
|
||||
class FakeCursor:
|
||||
def __init__(self, rows):
|
||||
self._rows = rows
|
||||
|
||||
def execute(self, *a, **k):
|
||||
pass
|
||||
|
||||
def fetchall(self):
|
||||
return self._rows
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *a):
|
||||
return False
|
||||
|
||||
|
||||
class FakeConn:
|
||||
def __init__(self, rows):
|
||||
self._rows = rows
|
||||
|
||||
def cursor(self):
|
||||
return FakeCursor(self._rows)
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
|
||||
ROWS = [
|
||||
("member:alice@example.org", 10.005),
|
||||
("member:bob@example.org", 5.005),
|
||||
("agent:infrastructure-bot", 1.5),
|
||||
("sandbox:throwaway", 0.02),
|
||||
("", 1.07), # spend rows with no team at all
|
||||
]
|
||||
|
||||
real_ready, real_connect = m.litellm_db_ready, m._pg_connect
|
||||
try:
|
||||
m.litellm_db_ready = lambda: True
|
||||
m._pg_connect = lambda: FakeConn(ROWS)
|
||||
t = m.coop_spend_totals()
|
||||
|
||||
print("1. the split adds up and covers every row")
|
||||
check(t["member_all_time_spend"] == 15.01,
|
||||
f"member side is the member: teams ($10.005 + $5.005 -> 15.01, got {t['member_all_time_spend']})")
|
||||
check(t["own_all_time_spend"] == 2.59,
|
||||
f"own side is everything else: agents, sandboxes, unattributed (got {t['own_all_time_spend']})")
|
||||
check(t["spend_all_time"] == round(t["member_all_time_spend"] + t["own_all_time_spend"], 2),
|
||||
"total == member + own, so no dollar is counted twice or dropped")
|
||||
check(t["spend_all_time"] == round(sum(r[1] for r in ROWS), 2),
|
||||
f"total is every SpendLogs row ({round(sum(r[1] for r in ROWS), 2)}), the same universe the money chart sums")
|
||||
|
||||
print("2. a failure to compute must not look like $0")
|
||||
def boom():
|
||||
raise RuntimeError("LiteLLM DB credentials not configured")
|
||||
m._pg_connect = boom
|
||||
check(m._coop_spend_totals_safe() == {},
|
||||
"the safe wrapper returns {} so callers omit the key instead of showing a fake zero")
|
||||
m.litellm_db_ready = lambda: False
|
||||
check(m._coop_spend_totals_safe() == {},
|
||||
"and it degrades the same way when the DB is simply not configured")
|
||||
finally:
|
||||
m.litellm_db_ready, m._pg_connect = real_ready, real_connect
|
||||
|
||||
print()
|
||||
if FAILS:
|
||||
print(f"coop spend totals: {len(FAILS)} FAILURE(S)")
|
||||
sys.exit(1)
|
||||
print("coop spend totals: OK — one definition, one number, no fake zeros")
|
||||
Reference in new issue
Block a user