Skip to content

Every request that starts reports how it ended - #25

Merged
fylorn merged 1 commit into
mainfrom
fix/every-request-ends
Sep 16, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/every-request-ends

Conversation

@fylorn

@fylorn fylorn commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

What this changes

Fixes three of the gaps listed in #24: a request that has emitted
RequestStarted now always gets exactly one terminal event.

Path Before Now
Client leaves before the response headers nothing RequestCancelled, no status, no usage
Phase-two rule denies after RequestStarted nothing RequestFailed, source denied
WebSocket session closes nothing RequestFinished, status 101
WebSocket upstream unreachable or breaks nothing RequestFailed, source upstream
WebSocket cut by the tool firewall nothing RequestFailed, source denied
  • tw_gateway::ending::Ending is created when RequestStarted is emitted,
    not when the response headers arrive. It gains responded(status),
    finished(status), and count(bytes) for byte-only accounting.
  • passthrough holds the ending while pipeline runs, and reports
    whatever pipeline returns: Err becomes RequestFailed with that
    error's source and message. The all-attempts-failed path no longer emits
    its own RequestFailed.
  • RequestCancelled.status becomes Option<u16>.
  • Source::slug is 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 passthrough reports the errors. Drop can only mean "this was
dropped", which here means the client left. If each return Err inside
the pipeline had to report its own failure, the first one somebody forgets
would not go unreported; it would be reported by Drop as a cancellation,
blaming the client for a policy denial. passthrough awaits 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 upstream
refused always carried upstream. It now carries the error's own source,
the same word as the x-thinkwatch-error header the client received.
Nothing reads source today (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 #24
move here from live.rs) runs a real gateway, upstream and client, and
asserts 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:

  • An Err left to Drop: the phase-two denial and the refused request
    come back as cancellations.
  • Drop silent before the headers: the early cancellation is lost.
  • The old direct RequestFailed kept alongside the new reporting: the
    refused request reports twice.
  • A closed WebSocket reporting nothing, and a cut reported as upstream:
    each fails its test.

Unit tests in ending.rs cover a drop before the headers (no status, no
usage), 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; the
35 MB memory warning is unchanged).

🤖 Generated with Claude Code

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>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

@fylorn
fylorn merged commit 40f33a5 into main Sep 16, 2026
2 checks passed
@fylorn
fylorn deleted the fix/every-request-ends branch September 16, 2026 15:24
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