Repository navigation
Fix: Record a failure row for a request whose body cannot be buffered - #1293
Conversation
A forward-proxy request whose body could not be buffered got its 413 (or 400) and recorded nothing, because the early return ran before the session was resolved. On plain HTTP that left no row at all. On a bridged tunnel, the path Claude Code takes to api.anthropic.com, the only rows were the tunnel's own open and a close saying 200, in the default bucket, so a request the client saw fail looked healthy. The return now records the request row and a paired failure row, in the session the client's headers name. bodyread.Failure classifies it: - over maxRequestBodySize: 413, proxy_error, with the announced size and the limit in the message - the client hung up mid-upload: 499, client_canceled - anything else unreadable: 400, a new kind, request_unreadable On a bridged tunnel the request row takes the tunnel's open with it, as any recorded request does, so no misleading close is recorded. Neither the pipeline nor a Finisher runs, so the request row carries no parse. From the rossoctl#1290 review: - TestForwardProxy_RequestBodyCapIs32MiB pins the cap with literals: exactly 32 MiB reaches the plugin and the upstream whole, and one byte more gets a 413. It fails with the reader at 16 MiB and with the constant at 64 MiB, which the constant-relative tests did not. - TestForwardProxy_BodyTooLarge now asserts the plugin never saw the body. Refs rossoctl#1250 Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Ed Snible <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 53 minutes. View limit detailsLimit details: You’ve used the included review currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (7)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
mrsabath
left a comment
There was a problem hiding this comment.
A genuine observability gap, fixed narrowly and with good evidence. I verified the mechanism rather than relying on the description.
The session-pairing works, and for the reason the comment gives. recordUnbufferedRequest sets pctx.OutboundSessionID = sid before calling recordOutboundResponseEvent, and that function reads pctx.OutboundSessionID first (server.go:1134), falling back to ActiveSession() then DefaultSessionID only when nothing was pinned. So both rows land in the same bucket, which is what makes the bridged test's "nothing in default" assertion hold.
The refactor is a pure move. I diffed the deleted inline SessionEvent literal against the extracted recordOutboundRequestEvent body: all 13 fields identical, same order, nothing added or omitted. That is what substantiates "this path records exactly the row the normal path does" — the normal path's behaviour is provably unchanged, so the extraction carries no regression risk.
The 499 classification follows an established pattern rather than inventing one. pipeline.StatusClientClosedRequest is a real constant (499) at core/pipeline/action.go:64, documented as "No client ever receives it — it has gone." More to the point, core/pipeline/snapshot.go:235's ExchangeFailure already returns exactly 499 plus Kind: "client_canceled" on ctx.Err() != nil. The new branch mirrors it, so the codebase now classifies an abandoned body-read and an abandoned exchange identically.
The /v1/usage claim rests on a real mechanism. TestRecord_UpstreamFailureCountsAsError (core/cost/usage/upstreamfailure_test.go) documents that a lone request event contributes 0 requests and 0 errors — counting happens entirely on the response half. That is precisely why recording a paired failure row, rather than only the request row, is what makes the failure visible. The fix is shaped correctly for the counting model it targets.
The MaxBytesError-before-context ordering is right, and tested. An overrun on an already-canceled context still reports 413 proxy_error, because the limit is what stopped the read — TestFailure's "overrun, client gone" case pins exactly that. Easy to get backwards; it isn't.
Test rigor is above average. The description reports negative controls: with the recording call removed all four tests fail with "recorded 0 event(s)", and TestForwardProxy_RequestBodyCapIs32MiB fails at both 16 MiB and 64 MiB. That last test addresses a blind spot it names honestly — the pre-existing tests compute their fixture from maxRequestBodySize, so they passed at any cap. Using literals is the point.
One nit inline, about discoverability rather than correctness.
Two smaller things worth acknowledging. The stale comment in bridgedopen_test.go is correctly re-pointed: it cited a body-read failure as the example of an unrecorded request, which this PR makes untrue, and it now names a plugin denial instead — easy to leave behind. And the "Not in this PR" section flags three real follow-ups unprompted: the reverse proxy has the same rowless return for its 1 MB limit, a truncated SSE response still records 200 with no error, and four deferred #1290 items. Scoping those out is the right call; the SSE one needs a status-semantics decision rather than a code change.
No secrets, no new external dependencies — only internal packages, and bodyread's new core/pipeline import introduces no cycle. CLAUDE.md's event-schema section is updated to match the new rows.
Areas reviewed: Go (forward proxy, bodyread), docs, tests, commit conventions, security
Commits: 1, signed off, imperative subject at 69 chars
CI: all 27 checks green (Spellcheck skipped)
One disclosure on method: I did not run the suite — no clone of this repo on my side — so the test-execution evidence is green CI plus your reported negative controls. I verified the integration logic, the status constants, the field-for-field refactor equivalence, and the usage-counting mechanism directly against source at 0ad6e68.
Nothing blocking. LGTM.
| } | ||
| return http.StatusRequestEntityTooLarge, &pipeline.EventError{Kind: "proxy_error", Message: msg} | ||
| } | ||
| if r.Context().Err() != nil { |
There was a problem hiding this comment.
Nit, on discoverability rather than correctness.
This branch makes Failure and Rejection deliberately disagree about status for the same error: Rejection returns 400 (the response the client is sent), while Failure returns 499 (the status recorded). That divergence is right — net/http cancels the request context on the read error and discards the response, so nobody receives the 400, and recording 499 reflects what happened rather than what was attempted. The doc comment says so, and TestFailure's "client gone" case pins it.
The nit is that the two functions are adjacent in one file, take the same err, and return statuses that differ by design, with neither signature nor call site saying which is which. A reader comparing them — or someone later "fixing the inconsistency" — has to reconstruct the reasoning from the prose in Failure's doc.
A half-line on Rejection pointing at Failure (something like "the status the client is sent; the status recorded is Failure's, which differs for a client that has gone") would make the asymmetry self-evident at both ends. Purely optional.
Summary
A forward-proxy request whose body couldn't be buffered got its 413 (or 400) and recorded nothing, because the early
returninserveOutboundran before the session was resolved.api.anthropic.com)200with no error, in thedefaultbucketOn the bridged path, a request the client saw fail looked healthy.
The
returnnow records the request row and a paired failure row, in the session the client's headers name. The newbodyread.Failureclassifies the failure:error.kindmaxRequestBodySizeproxy_error, e.g. "request body of 34603008 bytes is over the 33554432-byte buffer limit"client_canceled(net/http cancels the request context on the read error)request_unreadable, a new kind named after the existing log line and the client's JSON errorOn a bridged tunnel, the request row takes the tunnel's open with it, as any recorded request does, so no misleading close is recorded. Neither the pipeline nor a Finisher runs, so the request row has no model, tokens or cost. The 413 row counts as an error in
/v1/usage. The request-row construction moves intorecordOutboundRequestEvent, so this path records exactly the row the normal path does.Two items from the #1290 review are included:
TestForwardProxy_RequestBodyCapIs32MiBuses literals: exactly 32 MiB must reach both the plugin and the upstream whole, and one byte more must get a 413. The existing tests are computed from the constant, so they passed at any cap.TestForwardProxy_BodyTooLargenow asserts that the plugin never saw the body.CLAUDE.md's event-schema section now documents the new rows.Refs #1250.
Not in this PR
200with no error. That needs a decision on what status the row should carry, because/v1/usagecounts onlystatusCode >= 400as an error.Content-Lengthis already over the cap, the ext_proc docs wording, and the reverse-proxy comment nit.Test plan
TestForwardProxy_OversizeRequest_RecordsProxyError: plain HTTP, 32 MiB + 1 gives a request row and a 413proxy_errorrow whose message names the announced size and the limit.TestForwardProxy_BridgedOversizeRequest_RecordsProxyError: over the TLS bridge,sess-1holds the tunnel open, the request row and the 413 row, with no tunnel close and nothing indefault.TestForwardProxy_UnreadableRequestBody_RecordsFailure: a hangup mid-upload gives 499client_canceled, and a malformed chunked body gives 400request_unreadable.TestForwardProxy_RequestBodyCapIs32MiBfails with the reader set to 16 MiB, and with the constant set to 64 MiB.TestFailureinbodyreadcovers each classification, including a chunked overrun with no announced size.gofmt -l listenerandgo vet ./listener/...are clean,go test ./...incorepasses, and theforwardproxyandbodyreadpackages also pass under-race.Assisted-By: Claude (Anthropic AI) [email protected]