Repository navigation
fix: WebSocket requests report their route, and every row carries a real billing - #99
Merged
Merged
Conversation
…eal billing A WebSocket upgrade was routed like any other request but never emitted RequestRouted, so its row had no routing and an empty billing. The store read '' as per-token: a Codex session on a subscription account was counted as a per-token request with no usage, and the request drawer said it had no routing information. - The WebSocket path emits RequestRouted once the upstream handshake has an outcome, in the same shape as HTTP: the matched rule and group, and one attempt (served with 101, status for any other answer, error when it could not connect). Billing is the upstream's when it took the connection and per-token when it did not, the rule the HTTP path already applies when no upstream serves a request. - RequestStarted carries the billing of the upstream it is headed to. RequestRouted still decides, but not every request lives to see it: a client that leaves before any upstream answers, an upgrade abandoned mid-handshake, a phase-two deny. Those rows were '' too. - A locally answered request records "free". Its cost is a definite zero, which is what free means everywhere else. With every row carrying per-token, subscription, free or unknown, the no-price and no-usage counts compare billing = 'per-token' instead of also treating '' as per-token. The table shape is unchanged, so SCHEMA stays at 17. Rows written before this keep '' and drop out of those two counts. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fylorn
force-pushed
the
fix/ws-routing-billing
branch
from
September 22, 2026 05:32
bc5ca24 to
45f6d14
Compare
This was referenced Sep 22, 2026
Merged
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.
Why
A WebSocket upgrade is routed like any other request (
ws_upgraderuns the engine and picks an upstream), but it never emittedRequestRouted. Its row therefore had noroutingand an emptybilling, and the store reads''as per-token. Two visible results:What changed
RequestRoutedonce the upstream handshake has an outcome, in the same shape as HTTP: the matched rule and group, plus one attempt built by the samehop/hop_failedhelpers the pipeline uses. The attempt isservedwith 101 when the upstream accepts the upgrade,statuswith the status code when it answers anything else, anderrorwhen it cannot be reached. Billing is the upstream's when it took the connection andper-tokenwhen it did not, the same rule the HTTP path applies when no upstream serves a request. The event is emitted before the ending, so the recorder has it when the row is written. Building the request and dialing moved into oneconnectstep so that every outcome goes through one place.RequestStartedcarriesbilling: how the upstream it is headed to charges.RequestRoutedstill has the final say, but not every request lives to see it: a client that leaves before any upstream answers, an upgrade abandoned mid-handshake, and a phase-two deny all end without it. Those rows had''too, so without this the SQL below could not be tightened.free. Theircost_microsis already a definite 0, andfreeis the billing that means exactly that everywhere else. Local rows are excluded from every aggregate, so no count changes.NO_PRICE/NO_USAGEcomparebilling = 'per-token'instead ofbilling IN ('', 'per-token'), now that every row carries one of per-token, subscription, free or unknown.Protocol
RequestStartedgains a requiredbilling: String(same vocabulary asRequestRouted::billing). The desktop app's Rust side never constructsRequestStarted, so moving it to the release that contains this needs only the TypeScript mirror updated, plus the drawer'snoRoutingTip, which currently lists WebSocket connections among requests without routing information.Not changed
SCHEMAstays at 17: the table shape is the same. Rows written before this keep''and drop out of the no-price and no-usage counts. There is no migration.Test plan
cargo fmt --all -- --checkcargo clippy --workspace --all-targets -- -D warningscargo test --workspace(run locally withHTTP_PROXY/HTTPS_PROXYunset)scripts/smoke.sh: 51 passed, 0 failed, 1 over target (memory 35 MB against the 30 MB target, below the 40 MB regression line)New tests:
tw-gateway/tests/ws.rs: a session reports its rule, group, aserved/101 hop and the account's billing, before its ending; an upgrade the upstream refuses is astatushop billed per-token; an unreachable upstream is anerrorhop that says why.tw-gateway/tests/endings.rs: a request abandoned before the upstream answers has the headed-to upstream's billing on its start event and noRequestRouted.tw-storerecorder: a WebSocket session on a subscription upstream counts as subscription, not as missing money; the per-token one still counts as no usage; a request that ends before its route is reported keeps the billing it started with; a local answer recordsfree.tw-storedb: only a per-token row can count as no price or no usage.🤖 Generated with Claude Code