Compare commits

...
Author SHA1 Message Date
9qeklajc a383db5ee8 test: drop superseded billing regression tests 2026-07-24 21:08:06 +02:00
9qeklajc 6fcad8bcb9 Merge main into fix/zero-cost-fallback-billing 2026-07-24 21:02:06 +02:00
thefux 701c351ce7 fix: eliminate zero-cost billing fallback that gave free inference
When adjust_payment_for_tokens raised during a streaming finalize, the
code hardcoded total_msats=0 — giving users free inference and leaking
the reserved balance (never released). This was the most critical
billing bug still live on main.

Changes:
- Add _safe_finalize_billing helper that logs at CRITICAL, releases the
  reserved_balance (atomic clamp-to-zero pattern matching auth.py), and
  returns a non-zero cost dict using the reserved max_cost as best-effort
- Replace all 6 zero-cost fallback sites in base.py:
  - handle_streaming_chat_completion main finalize
  - handle_streaming_chat_completion finalize_db_only
  - handle_streaming_chat_completion finalize_without_usage
  - handle_streaming_messages_completion main finalize
  - handle_streaming_messages_completion finalize_db_only
  - handle_streaming_messages_completion finalize_without_usage
- Narrow except clauses from catch-all Exception to specific types
  (SQLAlchemyError, UpstreamError, OSError) so transient DB hiccups
  no longer silently result in free service
- Log at CRITICAL (was ERROR) so operators are alerted to money leaks
- Add 9 functional tests in test_zero_cost_fallback_fix.py covering:
  non-zero cost return, CRITICAL logging, reserved balance release,
  child key release, release failure handling, narrow exception types

All 5 existing RED tests in test_zero_cost_fallback.py now pass.
Full suite: 975 passed, 13 skipped, 0 failures.
2026-07-21 05:58:33 +00:00
thefux 7481ebd8cd Merge PR #622: fix: retry critical Cashu storage writes 2026-07-20 18:10:24 +00:00
thefux 7be1db8fd9 Merge PR #621: fix: propagate Cashu storage errors 2026-07-20 18:10:15 +00:00
thefux 4a4443f98f Merge PR #620: fix: guard fee payouts against crash double-payments 2026-07-20 18:10:06 +00:00
thefux fecf5b27b8 Merge PR #619: test: vulnerability-reproducing RED tests + coverage
# Conflicts:
#	.gitignore
#	tests/unit/test_coverage_admin.py
#	tests/unit/test_coverage_base2.py
#	tests/unit/test_coverage_payment_helpers.py
#	tests/unit/test_wallet_money_paths.py
2026-07-20 18:09:52 +00:00
thefux 59bcc3cbbf test: add 17 base.py coverage tests (41%→42%)
Tests for previously untested methods:
- _extract_upstream_error_message (5 tests): JSON error, text error, empty body
- on_upstream_error_redirect (2 tests): 402, 429 status codes
- _fold_cache_into_input_tokens (2 tests): no cache, preserves total
- get_cached_models / get_cached_model_by_id (2 tests)
- get_x_cashu_cost (2 tests): with/without usage data
- get_balance / create_account / refresh_models_cache / fetch_models (4 tests)

ruff: clean, mypy: clean, 884 pass, 10 RED, 14 skip
2026-07-17 16:27:07 +00:00
thefux 25f75643c2 test: add wallet money-path tests, strengthen billing RED tests
- New test_wallet_money_paths.py (10 tests): is_mint_connection_error,
  classify_redemption_error, store_cashu_transaction success path,
  get_balance, periodic task structure verification
- Fixed test_messages_streaming_no_silent_billing_failure:
  was  (always pass), now properly asserts
  the silent pass pattern must NOT exist
- ruff: all clean, mypy: all clean

10 RED failures (correct), 867 pass, 14 skip
2026-07-17 16:19:33 +00:00
thefux c68c1936d3 chore: fix ruff lint issues (29 fixes, 0 remaining) 2026-07-17 16:10:54 +00:00
thefux 49d285571e test: rewrite vulnerability tests as RED (assert correct behavior)
Rewrite vulnerability-documenting tests to assert CORRECT behavior
so they FAIL against current buggy main. These are TRUE TDD RED tests.

RED tests (10 failures — correct, these document live bugs):
- test_store_cashu_raises_on_db_failure (DB errors must propagate)
- test_retry_wrapper_exists (store_cashu_transaction_with_retry must exist)
- test_emergency_refund_no_try_except_pass (must not silently lose tokens)
- test_fee_payout_has_crash_guard (must have lock before pay)
- test_billing_error_must_not_hardcode_zero_cost (must not give free service)
- test_billing_error_must_release_reserved_balance (stuck funds)
- test_billing_error_catch_is_too_broad (narrow exception type)
- test_billing_error_must_log_critical (CRITICAL not ERROR)

New coverage tests (45 pass, zero regressions):
- test_coverage_base.py (17 tests): preparers, builders, injectors
- test_coverage_admin.py (11 tests): withdraw validation, slugs, auth
- test_coverage_proxy.py (13 tests): JSON parsing, model extraction

Coverage gains:
- middleware.py:  38% → 90%
- helpers.py:     52% → 60%
- proxy.py:       47% → 51%
- admin.py:       35% → 36%

Test suite: 857 pass, 10 RED failures, 14 skipped (zero regressions)
2026-07-17 16:05:56 +00:00
Paperclip Deployment Engineer eb108a4a5a test: add vulnerability-reproducing and coverage-filling tests
Adds 40 new tests across 5 test files that document critical bugs and
fill coverage gaps in the routstr-core codebase:

- test_emergency_refund_integrity.py (5 tests):
  Documents the try/except/pass vulnerability in emergency refund paths
  (base.py:3643-3653 and base.py:4607-4617) where DB store failures
  silently lose minted tokens. Verifies store_cashu_transaction catches
  all exceptions and send_token mints before DB persistence.

- test_zero_cost_fallback.py (6 tests):
  Documents the hardcoded zero-cost fallback (base.py:1012-1030) where
  exceptions from adjust_payment_for_tokens() result in total_msats=0,
  giving users free service with permanently reserved balances.

- test_db_and_payout_resilience.py (8 tests):
  Confirms store_cashu_transaction_with_retry was reverted (#600→#604).
  Documents the fee payout pay-then-reset crash window and wallet
  caching mechanism.

- test_coverage_middleware.py (11 tests):
  Fills middleware.py coverage gap (was 38%) — tests LoggingMiddleware,
  _should_log filters, request_id_context, and middleware exports.

- test_coverage_payment_helpers.py (10 tests):
  Fills payment/helpers.py coverage gap (was 52%) — tests
  check_token_balance, estimate_tokens, create_error_response,
  and image token calculation helpers.

All tests pass against current main (830 passed, 13 skipped).
2026-07-17 15:12:48 +00:00

Diff Content Not Available