Repository navigation
fix(sharing): reject an onboarding pair the platform cannot price - #201
Merged
Merged
Conversation
POST /api/sharings stored whatever `provider` / `model` the caller sent, and every call is then priced with `dao::get_model_price(&conn, &key.provider, model)`. When no `models` row matches, `settle_usage` falls back to `(0.0, 0.0)`: the gateway still serves the request (200, the upstream tokens are really spent) but the consumer is not charged, the sharer earns nothing and `usage_records.cost` stays 0 — a silently free call. Validate the pair against the `models` table before the INSERT and answer 400 when no priceable row exists, so an unpriceable key can never be onboarded. The check is the `models` row, not membership in `[[providers]]`: openai / anthropic / google / xai only exist in `[[models]]`, so a providers-table check would reject legitimate keys. Zero-pricing for a key whose model is deleted later is unchanged — that is the documented behaviour, not this path. Tests: a provider outside the catalog -> 400, a mistyped model -> 400, neither row is stored (the check runs before the INSERT), and a catalog pair -> 200 as the positive control.
11 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /api/sharings(routes/sharing.rs::create) validated only thatmodelandkeywere non-empty, then storedprovider/plan/modelverbatim. But every call is billed withdao::get_model_price(&conn, &key.provider, model)(gateway.rs:182→dao.rs:190,WHERE provider = ?1 AND model = ?2), andkey.provideris caller-supplied — so a pair with nomodelsrow takes theNone => (0.0, 0.0)arm ofsettle_usageand the call is silently free: the request is served normally (200, the upstream tokens are really spent), yet the consumer is not charged, the sharer earns nothing, andusage_records.coststays 0.This PR rejects an onboarding whose
(provider, model)the platform cannot price, so such a key can never enter the pool.Related Issue
(no open issue)
Changes
routes/sharing.rs::create— after the existing non-empty check (and before theINSERT), look up the pair inmodelsand answer400 {"error": "provider 与 model 不在模型目录中,无法计价"}when there is no priceable row.modelsrow, not membership in[[providers]]:openai/anthropic/google/xaihave[[models]]rows but no[[providers]]row, so a providers-table check would reject legitimate keys. Priceability is exactly what the settlement path requires.ui/change, so no example-file sync is needed.Tests
cargo test— 189 passed (was 188; the new test is the only added case)cargo fmt --check— cleancargo clippy --all-targets -- -D warnings— cleancreate_rejects_unpriceable_provider_model: provider outside the catalog → 400; mistyped model → 400; neither is stored (the guard precedes theINSERT); a catalog pair → 200 and one row stored (positive control).assertion left: 200, right: 400) while the rest of the module stays green.Checklist
fix/sharing-onboarding-priceablefollows the conventionfix(sharing): …)Out of scope (deliberate)
planthat is not in config[[plans]]is still accepted: it is storedstatus='on'whileresolve_outboundalways returnsNone, so the key is unroutable forever (onboard 200 / use 503) andlist_models_with_availabilitystill counts it. That is a separate symptom (routing, not pricing) and gets its own change.settle_usage'sNone => (0.0, 0.0)arm is untouched: a key whose model is deleted later legitimately settles 0.