Skip to content

Lift the two sideband readers out of the gateway - #126

Merged
fylorn merged 2 commits into
mainfrom
refactor/tw-wire
Sep 23, 2026
Merged

fylorn merged 2 commits into
mainfrom
refactor/tw-wire

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

First step of the shared-layer work between this repo and the enterprise gateway.

What moves

usage.rs (473 lines) and bodies.rs (155 lines) leave tw-gateway for a new tw-wire crate. 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-gateway for real coupling, not just its use lines:

module lines actual coupling verdict
usage.rs 473 none moved
bodies.rs 155 none moved
health.rs 287 none next
toolwall.rs 1190 tw_scan::rules after rules are split
ending.rs 466 bodies + usage + tw_types after this
probe.rs 294 4 inline crate::chatgpt / crate::forward needs splitting first

probe.rs looks clean by its use lines 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:

  • its token counter is 106 lines of latin/4 + cjk/2 character estimation
  • streaming usage is read off ChatCompletionChunk.usage, i.e. whatever survived the provider adapter — for Gemini (cumulative usageMetadata per frame) and Anthropic (message_start + message_delta) that is frequently nothing
  • no vendor-specific input accounting, so a cached prefix gets charged at input price twice, because OpenAI and Gemini fold cache hits into the input count and Anthropic does not

That last one is a billing correctness issue on the side that actually invoices customers.

Notes

tokio is declared directly instead of workspace = 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. One mpsc::Sender needs sync.

cargo clippy --workspace --all-targets -- -D warnings clean, full test suite green.

🤖 Generated with Claude Code

fylorn and others added 2 commits September 23, 2026 15:17
`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>
@fylorn
fylorn merged commit 6a852b5 into main Sep 23, 2026
1 check passed
@fylorn
fylorn deleted the refactor/tw-wire branch September 23, 2026 07:29
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