Skip to content

feat(aibridge/x/proxy): capture HTTP bodies for replay - #30398

Merged
pawbana merged 3 commits into
mainfrom
pawel/issue-29726-request-replay
Nov 9, 2026
Merged

pawbana merged 3 commits into
mainfrom
pawel/issue-29726-request-replay

Conversation

@pawbana

@pawbana pawbana commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Adds HTTP body capture under aibridge/x/proxy/replaybody. NewRequest and NewResponse replace the original body with an io.ReadCloser that retains read bytes in the returned TeeBuffer.
Request retries replay the retained bytes through r.GetBody().
TeeBuffer.Wait returns buffered bytes without reading the original source.

Enables full duplex so Go’s HTTP/1 server can send an early upstream response without first draining the client’s unfinished upload.

Generated by Coder Agents on behalf of @pawbana.

@pawbana
pawbana force-pushed the pawel/issue-29726-request-replay branch from 3125c17 to 01dbcc0 Compare October 6, 2026 11:15
@pawbana pawbana changed the title feat(aibridge/x/proxy): add lazy request body replay feat(aibridge/x/proxy): capture streaming bodies for replay Oct 6, 2026
@pawbana
pawbana force-pushed the pawel/issue-29726-request-replay branch 4 times, most recently from ed49523 to 1885657 Compare October 7, 2026 10:26
Base automatically changed from pawel/issue-29726-validation to main October 7, 2026 16:51
@pawbana
pawbana force-pushed the pawel/issue-29726-request-replay branch 2 times, most recently from e3e8d44 to b7a42c3 Compare November 2, 2026 11:09
@pawbana pawbana changed the title feat(aibridge/x/proxy): capture streaming bodies for replay feat(aibridge/x/proxy): capture HTTP bodies for replay Nov 2, 2026
@pawbana
pawbana force-pushed the pawel/issue-29726-request-replay branch 16 times, most recently from 647b3dd to bb0e85a Compare November 4, 2026 14:08
@pawbana
pawbana marked this pull request as ready for review November 4, 2026 14:12
Comment thread aibridge/x/proxy/replaybody/buffer.go Resolved
}

func (w *responseWriter) WriteHeader(code int) {
// 1xx replies are informational, and 101 carries Connection: Upgrade.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Do we need the comment about 101 status code?

Comment thread aibridge/x/proxy/replaybody/buffer.go Outdated
response.Body = http.NoBody
return &response, newBuffer(ctx, nil)
}
if response.Trailer == nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, when capture reads the body to EOF in its own goroutine, the Go HTTP transport writes the trailers into this shared Trailer map during that read. Could that race with a consumer like ReverseProxy?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, yes there would be a race.
I removed the eager read and changed buffer to lazy read approach. Buffer now is filled by ReverseProxy reading the request / response body.
Also removed most of the HTTP logic from buffer, the 101 switching protocols check is already done in forwardingHandler.checkRequest also moved duplex enablement to handler.

Comment thread aibridge/x/proxy/replaybody/buffer.go Outdated
func (s *bodySource) Close() error {
if s.controller != nil && !s.eof.Load() {
//nolint:errcheck // Deadline interruption is best-effort.
_ = s.controller.SetReadDeadline(time.Now())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check the error here? 🤔 IIUC, Stop can run while the client is still sending data, so this deadline is the only thing that unblocks the in-progress body Read. If it fails, could Stop hang?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this, now original source is read in separate go-routine so Close or Stop will not hang on in-flight read. The separate go-routine worst case will timeout with server configured ReadTimeout.

@pawbana
pawbana force-pushed the pawel/issue-29726-request-replay branch 2 times, most recently from 7bd6e6f to b88c412 Compare November 6, 2026 13:30
@pawbana
pawbana requested a review from ssncferreira November 6, 2026 13:30
@pawbana
pawbana force-pushed the pawel/issue-29726-request-replay branch from b88c412 to 065c279 Compare November 6, 2026 13:54
@pawbana
pawbana force-pushed the pawel/issue-29726-request-replay branch from 830e62d to 26b5be9 Compare November 9, 2026 09:29
@pawbana
pawbana temporarily deployed to main November 9, 2026 09:29 — with GitHub Actions Inactive
@pawbana
pawbana temporarily deployed to main November 9, 2026 09:37 — with GitHub Actions Inactive
@pawbana
pawbana temporarily deployed to main November 9, 2026 09:40 — with GitHub Actions Inactive
@pawbana
pawbana temporarily deployed to main November 9, 2026 09:41 — with GitHub Actions Inactive
@linear-code

linear-code Bot commented Nov 9, 2026

Copy link
Copy Markdown

AIGOV-786

@pawbana
pawbana temporarily deployed to main November 9, 2026 09:59 — with GitHub Actions Inactive

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 just a few questions

// Close marks unfinished capture as interrupted and closes the source. It
// does not wait for an in-flight source read, whose bytes are dropped. It
// returns the source close error, cached for later calls.
func (b *TeeBuffer) Close() error {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, for requests this doesn't stop an in-flight teeRead, since the source is a NopCloser. If the provider returns an error while the client is still uploading, the gateway relays it and the handler returns, but teeRead may still be reading the client's body after that. Could this be a problem?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An in-flight teeRead can outlive forwarding, but the original request body is owner by server and HTTP server cleanup interrupts pending reads.
There is one trade off, early termination can result in buffer with partially read body, which may prevent recording, eg. prompt.
If we decide that we need full request body before forwarding, handler could read it. TeeBuffer would still be useful for response.

return b
}

// Wait returns the retained bytes once capture finishes. A nil error means

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With lazy capture, IIUC Wait only returns once the body is fully read or Close is called. If upstream stops reading early and someone calls Wait before Close, could it block forever?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait should be given a context with a timeout. Even if not handler owns the buffer and is responsible for closing it.

@pawbana
pawbana merged commit d430a62 into main Nov 9, 2026
45 checks passed
@pawbana
pawbana deleted the pawel/issue-29726-request-replay branch November 9, 2026 13:05
@pawbana
pawbana temporarily deployed to main November 9, 2026 13:05 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 9, 2026

This branch was previously deployed

1 inactive deployment
main — 26b5be9f Deployed Nov 9, 2026 by pawbana via cla #252486
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants