Repository navigation
Every request that starts reports how it ended - #25
Merged
Merged
Conversation
A request that had emitted RequestStarted could still end without a terminal event on three paths, each leaving a row that is never written and a live row that stays in progress: - The client left before the response headers arrived: a non-streaming request, or a slow relay. hyper drops the whole handler future at the await on the upstream, so nothing after that await runs. - A phase-two rule denied the request. That check runs inside the attempt loop, after RequestStarted, and returned its error unreported. - A WebSocket session. The proxy emitted RequestStarted and nothing when the connection closed. The ending now exists from the moment RequestStarted is emitted. `passthrough` holds it while the pipeline runs, because it is the one place that can tell a pipeline that returned an error from one that was dropped: an error is reported as RequestFailed with the error's own source and message, and a drop is reported by Drop as RequestCancelled. Reporting from each `return Err` inside the pipeline instead would turn every path someone forgets into a cancellation blamed on the client. Once the upstream responds, the ending moves into the response body as before. On the WebSocket path it moves into the upgraded connection, which reports RequestFinished (101) when either side closes the session, RequestFailed with `upstream` when the upstream breaks or cannot be dialled, and with `denied` when the tool firewall cuts the connection. RequestCancelled.status becomes optional: a request abandoned before the headers has no status, and reporting 0 or 499 would be invented. A request every upstream refused now fails with the error's own source instead of always `upstream`, so a 429 reports `rate_limited`, the same word as the x-thinkwatch-error header the client received. WebSocket sessions report no usage. One connection carries several responses, the sniffer keeps the maximum of each field, and the upgrade request names no model, so any number would be wrong. The ending tests move out of live.rs into endings.rs, which also covers the three paths above. Each new test was run against the broken version it guards: letting an error fall into Drop turns the phase-two denial and the refused request into cancellations; Drop staying silent before the headers loses the early cancellation; keeping the old direct failure emit reports the refused request twice; a closed WebSocket reporting nothing, and a cut reported as an upstream failure, each fail their test. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Thanks for the PR. One thing before review: this targets
No need to close and reopen: click Edit next to the PR title and change the base branch to See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract. |
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.
What this changes
Fixes three of the gaps listed in #24: a request that has emitted
RequestStartednow always gets exactly one terminal event.RequestCancelled, no status, no usageRequestStartedRequestFailed, sourcedeniedRequestFinished, status 101RequestFailed, sourceupstreamRequestFailed, sourcedeniedtw_gateway::ending::Endingis created whenRequestStartedis emitted,not when the response headers arrive. It gains
responded(status),finished(status), andcount(bytes)for byte-only accounting.passthroughholds the ending whilepipelineruns, and reportswhatever
pipelinereturns:ErrbecomesRequestFailedwith thaterror's source and message. The all-attempts-failed path no longer emits
its own
RequestFailed.RequestCancelled.statusbecomesOption<u16>.Source::slugis public, since it is the event's vocabulary too.Why
Each path left the recorder holding a request it never writes, and the
desktop app showing a row in progress indefinitely.
Why
passthroughreports the errors.Dropcan only mean "this wasdropped", which here means the client left. If each
return Errinsidethe pipeline had to report its own failure, the first one somebody forgets
would not go unreported; it would be reported by
Dropas a cancellation,blaming the client for a policy denial.
passthroughawaits the pipeline,so it is the one place that sees the difference between a returned error
and a dropped future, and it handles every error path, including ones
added later.
Why a 429 now says
rate_limited. The failure after every upstreamrefused always carried
upstream. It now carries the error's own source,the same word as the
x-thinkwatch-errorheader the client received.Nothing reads
sourcetoday (neither the recorder nor the desktop app),so this corrects the event without changing any stored data.
Why WebSocket sessions report no usage. One connection carries several
responses, each reporting its own usage. The sniffer keeps the maximum of
each field, which would report one response's numbers as the whole
session's, and the upgrade request names no model to price them with.
Recording no usage keeps these rows in the "no usage" state rather than
presenting a wrong number.
How it was checked
crates/tw-gateway/tests/endings.rs(new; the three ending tests from #24move here from
live.rs) runs a real gateway, upstream and client, andasserts exactly one ending for each case in the table, plus the three from
#24.
Each new test was run against the broken version it guards:
Errleft toDrop: the phase-two denial and the refused requestcome back as cancellations.
Dropsilent before the headers: the early cancellation is lost.RequestFailedkept alongside the new reporting: therefused request reports twice.
upstream:each fails its test.
Unit tests in
ending.rscover a drop before the headers (no status, nousage), and byte-only counting not turning frames into usage. A recorder
test covers a cancellation with no status.
Gates:
cargo fmt --check,cargo clippy --workspace --all-targets -D warnings,cargo test --workspace(1,151 passed),./scripts/smoke.sh(47 passed; the35 MB memory warning is unchanged).
🤖 Generated with Claude Code