fix(tracker): close alpha accounting review gaps
Address post-review blockers from the alpha hardening diff: charge non-stream completions that omit usage, include prompt tokens in max_charge_per_request enforcement, and wire the CLI to a persisted local registry contract by default. Verification: .venv/bin/python -m compileall -q packages tests; focused billing/CLI checks passed; full pytest: 314 passed, 3 skipped, 1 env failure from existing meshnet-node on port 7000.
This commit is contained in:
@@ -21,6 +21,7 @@ from meshnet_tracker.server import (
|
||||
_billable_non_stream_tokens,
|
||||
_billable_stream_tokens,
|
||||
_estimate_prompt_tokens,
|
||||
_observed_non_stream_completion_tokens,
|
||||
_observed_stream_tokens,
|
||||
)
|
||||
|
||||
@@ -99,6 +100,14 @@ def test_non_stream_billable_tokens_cap_usage_by_request_bound():
|
||||
assert _billable_non_stream_tokens(payload, request) == 5
|
||||
|
||||
|
||||
def test_non_stream_billable_tokens_fallback_when_usage_missing():
|
||||
payload = {"choices": [{"message": {"role": "assistant", "content": "ok now"}}]}
|
||||
request = {"messages": [{"role": "user", "content": "hello tracker"}], "max_tokens": 10}
|
||||
|
||||
assert _observed_non_stream_completion_tokens(payload) == 2
|
||||
assert _billable_non_stream_tokens(payload, request) == 4
|
||||
|
||||
|
||||
def test_stream_billable_tokens_allow_usage_to_lower_observed_count_only():
|
||||
observed_payload = {
|
||||
"choices": [{
|
||||
@@ -557,7 +566,12 @@ def test_proxy_chat_rejects_request_above_spend_cap_before_routing():
|
||||
r.read()
|
||||
|
||||
with pytest.raises(urllib.error.HTTPError) as exc_info:
|
||||
_chat(tracker_url, api_key="capped-client", max_tokens=1000)
|
||||
_chat(
|
||||
tracker_url,
|
||||
api_key="capped-client",
|
||||
messages=[{"role": "user", "content": " ".join(["prompt"] * 1000)}],
|
||||
max_tokens=1,
|
||||
)
|
||||
body = json.loads(exc_info.value.read())
|
||||
assert exc_info.value.code == 402
|
||||
assert body["error"]["code"] == "spend_cap_exceeded"
|
||||
|
||||
Reference in New Issue
Block a user