Repository navigation
Lift the two sideband readers out of the gateway - #126
Merged
Merged
Conversation
`usage` and `bodies` sit in tw-gateway, but neither knows anything about this gateway. Zero `crate::` references between them and the rest, zero users outside tw-gateway, and one external dependency each — serde_json and bytes. They are general wire-format machinery filed under a binary that happens to have been written first. The enterprise gateway needs both and has neither. Its token counter is 106 lines of `latin/4 + cjk/2` character estimation, and it reads streaming usage off whatever the provider adapter left on a chat chunk, so anything the adapter dropped is simply gone. `usage` here reads the upstream's own numbers across all four formats in both modes, and subtracts cache hits where the vendor folds them into the input count — OpenAI and Gemini do, Anthropic does not. Billing off the estimate charges the cached prefix at input price a second time. So they move to `tw-wire`, which either side can depend on. Both files move unchanged; only the paths that referenced them changed. tokio is declared here rather than taken from the workspace: the workspace copy carries net, signal and timers, and a crate meant to be pulled in from outside should decide for itself what it drags along. A single `mpsc::Sender` needs `sync`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Sep 23, 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.
First step of the shared-layer work between this repo and the enterprise gateway.
What moves
usage.rs(473 lines) andbodies.rs(155 lines) leavetw-gatewayfor a newtw-wirecrate. Both files move unchanged — the diff is pure rename plus the paths that referenced them.Why these two first
I audited every candidate module in
tw-gatewayfor real coupling, not just itsuselines:usage.rsbodies.rshealth.rstoolwall.rstw_scan::rulesending.rstw_typesprobe.rscrate::chatgpt/crate::forwardprobe.rslooks clean by itsuselines and is not — it reaches into the ChatGPT sign-in path inline. Worth recording, since that is exactly the kind of thing that makes a "simple" move expensive halfway through.What the enterprise side gains
It needs both of these and has neither:
latin/4 + cjk/2character estimationChatCompletionChunk.usage, i.e. whatever survived the provider adapter — for Gemini (cumulativeusageMetadataper frame) and Anthropic (message_start+message_delta) that is frequently nothingThat last one is a billing correctness issue on the side that actually invoices customers.
Notes
tokiois declared directly instead ofworkspace = true. The workspace copy carries net, signal and timers; a crate meant to be depended on from another repository should decide what it drags along. Onempsc::Senderneedssync.cargo clippy --workspace --all-targets -- -D warningsclean, full test suite green.🤖 Generated with Claude Code