Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a request's capture for Recent calls is copied once, not twice

yetone47ca891b1bb0
diff --git a/internal/gateway/capture.go b/internal/gateway/capture.go
index e53a2a15d..968255ae9 100644
--- a/internal/gateway/capture.go
+++ b/internal/gateway/capture.go
@@ -39,10 +39,11 @@ func (c *capturedBody) add(p []byte) {
func (c *capturedBody) text() string { return c.buf.String() }
+// captureRequestBody is a request's first callBodyLimit bytes, copied once:
+// through a capturedBody it was copied into a buffer and then again into
+// its string, 512 KiB for every request of 256 KiB or more.
func captureRequestBody(p []byte) (string, bool) {
- var c capturedBody
- c.add(p)
- return c.text(), c.truncated
+ return string(p[:min(len(p), callBodyLimit)]), len(p) > callBodyLimit
}
type captureResponseWriter struct {
diff --git a/internal/gateway/capture_test.go b/internal/gateway/capture_test.go
index 73bae7d3a..35505a067 100644
--- a/internal/gateway/capture_test.go
+++ b/internal/gateway/capture_test.go
@@ -12,6 +12,21 @@ func TestCaptureRequestBodyLimit(t *testing.T) {
if len(got) != callBodyLimit || !truncated {
t.Fatalf("captured %d bytes, truncated=%v", len(got), truncated)
}
+ for _, n := range []int{0, 5, callBodyLimit} {
+ in := []byte(strings.Repeat("y", n))
+ if got, truncated := captureRequestBody(in); got != string(in) || truncated {
+ t.Fatalf("%d bytes captured as %d, truncated=%v", n, len(got), truncated)
+ }
+ }
+ // copied once: the capture is all a request's capture allocates
+ if n := allocated(func() { captureRequestBody(body) }); n > callBodyLimit*5/4 {
+ t.Fatalf("capturing %d bytes allocated %d", callBodyLimit, n)
+ }
+ // and it is a copy: the body's memory is not the capture's
+ body[0] = 'z'
+ if got[0] != 'x' {
+ t.Fatal("the capture reads the body's memory")
+ }
}
func TestCaptureResponseWriterPreservesResponse(t *testing.T) {