Skip to content

Harden estimator/CLI, 4x tests (43->202), 4x demos (5->20)#5

Open
cognis-digital wants to merge 1 commit into
mainfrom
enhance/tests-demos-hardening
Open

Harden estimator/CLI, 4x tests (43->202), 4x demos (5->20)#5
cognis-digital wants to merge 1 commit into
mainfrom
enhance/tests-demos-hardening

Conversation

@cognis-digital

Copy link
Copy Markdown
Owner

What

A depth pass on tokenmeter: real error handling and bug fixes, ~4.7x the tests, and 4x the runnable demo scenarios. The public API is unchanged.

Fixes / hardening

  • estimate() rejected nothing. Negative input_tokens / output_tokens produced negative (nonsensical) costs. It now raises a clear ValueError. Via the CLI (count -o -5) this had been an uncaught traceback; the CLI now maps it to error: ... and exit code 2.
  • add_model() accepted garbage. Blank names, non-numeric or negative prices, and non-positive context windows were silently stored — one mispriced model then corrupts every downstream estimate and the context_used_pct division. All are now rejected up front.
  • count_tokens() tolerates None and raises TypeError on non-str input instead of crashing inside the regex.
  • compare CLI guards against an empty ranking (would have IndexErrord on ests[0]).
  • Identity constants. TOOL_NAME / TOOL_VERSION are now defined in core, so the documented from tokenmeter.core import TOOL_NAME, TOOL_VERSION path actually resolves instead of silently falling back in __init__.

Tests: 43 -> 202

New tests/test_core_edge.py, test_cli_edge.py, test_identity.py, test_demos_behavior.py cover tokenizer boundaries (empty/None/unicode/ceildiv/digit grouping), pricing validation, budget-gate boundaries (exactly-at-limit, multi-violation), model compare (ties/subsets/empty/unknown), aggregation (empty/generator/rounding), every CLI subcommand x format x documented exit code, stdin piping, malformed input, and per-demo behavior/idempotence.

Demos: 5 -> 20

Fifteen new offline, audience-varied scenarios: chat-history cost, context-window guard, AI diff review, few-shot tax, model cascade/router, agent step budget, prompt diet, RAG top-k sweep, multi-model portfolio, output-length forecast, prompt-library audit, custom/self-hosted pricing, JSON pipeline, cost-regression guard, and a full-month forecast. demos/run_all.py and docs/DEMOS.md updated. Each exits 0 and prints a cost figure with no network/keys.

python -m pytest -q      # 202 passed
python demos/run_all.py  # all 20, exit 0

Depth pass on tokenmeter: real error handling + fixes, ~4.7x the tests,
and 4x the runnable demo scenarios. Public API is unchanged.

Fixes / hardening (tokenmeter/core.py, tokenmeter/cli.py):
- estimate() now rejects negative input_tokens / output_tokens with a clear
  ValueError instead of silently producing negative (nonsensical) costs.
- add_model() validates its inputs: blank name, non-numeric or negative
  prices, and non-positive context windows are rejected up front, so a
  mispriced model can no longer corrupt every downstream estimate.
- count_tokens() tolerates None and raises TypeError on non-str input rather
  than crashing deep in the regex.
- CLI maps ValueError to a clean `error: ...` message and exit code 2 (was an
  uncaught traceback), and guards the compare path against an empty ranking.
- Define TOOL_NAME / TOOL_VERSION in core so the documented
  `from tokenmeter.core import TOOL_NAME, TOOL_VERSION` path resolves instead
  of silently falling back in __init__.

Tests: 43 -> 202. New tests/test_core_edge.py, test_cli_edge.py,
test_identity.py, test_demos_behavior.py cover tokenizer boundaries, pricing
validation, budget-gate boundaries, model-compare ties/subsets/empty,
aggregation, every CLI subcommand x format x exit code, stdin, malformed
input, and per-demo behavior.

Demos: 5 -> 20 runnable scenarios (chat history, window guard, diff review,
few-shot tax, model cascade, agent budget, prompt diet, RAG top-k sweep,
portfolio rollup, output-length forecast, library audit, custom pricing,
JSON pipeline, cost-regression guard, monthly forecast). run_all.py and
docs/DEMOS.md updated; each exits 0 and prints a cost figure offline.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant