Skip to content

fix: WebSocket requests report their route, and every row carries a real billing - #99

Merged
fylorn merged 1 commit into
mainfrom
fix/ws-routing-billing
Sep 22, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/ws-routing-billing

Conversation

@fylorn

@fylorn fylorn commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Why

A WebSocket upgrade is routed like any other request (ws_upgrade runs the engine and picks an upstream), but it never emitted RequestRouted. Its row therefore had no routing and an empty billing, and the store reads '' as per-token. Two visible results:

  • the desktop app's request drawer says a WebSocket connection has no routing information;
  • a Codex session on a subscription account is counted as a per-token request with no usage, so it shows up in "N with no usage" (money supposedly missing) instead of "N on subscription".

What changed

  • WebSocket requests emit RequestRouted once the upstream handshake has an outcome, in the same shape as HTTP: the matched rule and group, plus one attempt built by the same hop / hop_failed helpers the pipeline uses. The attempt is served with 101 when the upstream accepts the upgrade, status with the status code when it answers anything else, and error when it cannot be reached. Billing is the upstream's when it took the connection and per-token when 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 one connect step so that every outcome goes through one place.
  • RequestStarted carries billing: how the upstream it is headed to charges. RequestRouted still 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.
  • Local answers record free. Their cost_micros is already a definite 0, and free is the billing that means exactly that everywhere else. Local rows are excluded from every aggregate, so no count changes.
  • NO_PRICE / NO_USAGE compare billing = 'per-token' instead of billing IN ('', 'per-token'), now that every row carries one of per-token, subscription, free or unknown.

Protocol

RequestStarted gains a required billing: String (same vocabulary as RequestRouted::billing). The desktop app's Rust side never constructs RequestStarted, so moving it to the release that contains this needs only the TypeScript mirror updated, plus the drawer's noRoutingTip, which currently lists WebSocket connections among requests without routing information.

Not changed

  • SCHEMA stays 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.
  • Requests that end before routing concludes (client leaves before an upstream answers, phase-two deny) still have no routing. They now carry the headed-to upstream's billing.

Test plan

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test --workspace (run locally with HTTP_PROXY/HTTPS_PROXY unset)
  • 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, a served/101 hop and the account's billing, before its ending; an upgrade the upstream refuses is a status hop billed per-token; an unreachable upstream is an error hop 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 no RequestRouted.
  • tw-store recorder: 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 records free.
  • tw-store db: only a per-token row can count as no price or no usage.

🤖 Generated with Claude Code

…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
fylorn force-pushed the fix/ws-routing-billing branch from bc5ca24 to 45f6d14 Compare September 22, 2026 05:32
@fylorn
fylorn merged commit 6fdc930 into main Sep 22, 2026
1 check passed
@fylorn
fylorn deleted the fix/ws-routing-billing branch September 22, 2026 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant