diff options
| author | Danilo M. <danix@danix.xyz> | 2026-08-09 16:38:44 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-08-09 16:38:44 +0200 |
| commit | 46866c570c719ed02c8b43d688a2725bdcf5011c (patch) | |
| tree | a018cf306f396de7098573ebd449e44b622775a7 /test_llamachat.py | |
| parent | 5ffbe947736684df2b7a8dd1d647e16c080b0b1d (diff) | |
| download | llamachat-46866c570c719ed02c8b43d688a2725bdcf5011c.tar.gz llamachat-46866c570c719ed02c8b43d688a2725bdcf5011c.zip | |
fix: reject non-finite provider prices, and read them as unpriced
A nan or inf in [providers.*] survived float() and reached the cost
arithmetic, rendering $nan or, for -inf, a negative cost. Worse than a
display wart: is_priced answered True, so the readout took the priced
branch and showed a poisoned figure in the one state the ? is there to
admit it cannot say.
Guarded at both ends. providers._number mirrors models._get, which
already covered the models.ini writer. is_priced states its own
predicate completely rather than trusting its callers, since the dialog
is a third writer and the file is documented for hand-editing.
Records why float money stays, and that exact-equality assertions in the
cost test hold for the chosen prices rather than in general.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'test_llamachat.py')
| -rwxr-xr-x | test_llamachat.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/test_llamachat.py b/test_llamachat.py index 695a303..5f96a80 100755 --- a/test_llamachat.py +++ b/test_llamachat.py @@ -960,6 +960,32 @@ def test_provider_parsing(): assert parsed["local"].ctx_size is None assert parsed["local"].price_in is None + # nan and inf survive float() and would reach the cost arithmetic, where + # they render as "$nan" or "$-inf" in the *priced* branch: the readout + # inventing a figure in the one state built to admit it cannot say. + # Unknown is the honest answer, exactly as in models.ini. + nonfinite = providers.parse( + { + "providers": { + "p": { + "base_url": "http://x.example.org", + "price_in": float("nan"), + "price_out": float("inf"), + }, + "n": { + "base_url": "http://y.example.org", + "price_in": float("-inf"), + "ctx_size": 8192, + }, + } + } + ) + assert nonfinite["p"].price_in is None + assert nonfinite["p"].price_out is None + # A bad price must not take the good ctx_size down with it. + assert nonfinite["n"].price_in is None + assert nonfinite["n"].ctx_size == 8192 + # A [providers.local] that omits base_url inherits the bare one rather # than shadowing the local provider out of existence. partial = providers.parse( @@ -1579,6 +1605,14 @@ def test_metadata_and_cost(): ) == 0.0 # Cost: prompt at the input rate, completion at the output rate. + # + # The exact == below is safe for these particular prices, not + # because the arithmetic is exact in general: n * p / n round-trips + # to p for 0.6 and 5.0, and 0.6 + 5.0 is exactly 5.6, the same way + # 0.1 + 0.2 is famously not 0.3. Adding a price here and asserting + # its exact total can fail in the last bits and look like a costing + # bug when it is only float representation. Use + # abs(cost - expected) < 1e-9 for any price you add. rows = [ {"model": "together:other", "prompt_tokens": 1_000_000, "completion_tokens": 1_000_000}, @@ -1640,6 +1674,19 @@ def test_metadata_and_cost(): # Whether a model can be priced at all decides ? versus blank. assert models.is_priced("together:other", table, store) is True assert models.is_priced("free:anything", table, store) is False + + # A non-finite price is one no cost can be computed from, so the + # answer is "not priced" rather than a True that sends the readout + # down the priced branch to show "$nan". Both of today's writers + # filter these out already; this states is_priced's own predicate + # completely, for Task 10's dialog and Task 15's hand-editing. + poisoned = providers.parse( + {"providers": {"p": {"base_url": "http://x.example.org", + "api_key": "env:X"}}} + ) + poisoned["p"].price_in = float("nan") + poisoned["p"].price_out = float("-inf") + assert models.is_priced("p:x", poisoned, store) is False # Local is free, never unpriced. assert models.is_billable("gemma4", table) is False assert models.is_billable("free:anything", table) is False # no api_key |
