diff --git a/internal/gateway/prev_response.go b/internal/gateway/prev_response.go index 4a6cc655a..26a3185ff 100644 --- a/internal/gateway/prev_response.go +++ b/internal/gateway/prev_response.go @@ -258,13 +258,17 @@ func previousResponse(w http.ResponseWriter, r *http.Request, from provider.Prot if json.Unmarshal(q["store"], &store) == nil && !store { return w, r, body, nothing // the client asked that nothing be kept } - // what this turn adds: its whole body, which its items hold - own := len(body) + // what this turn adds: its input, copied out of the body. A view held + // the whole body, tools and instructions with it, on every turn kept, + // while own counted only the items, so the cap bounded far less than + // was held. + kept := ownItems(fresh) if ask.known { - own = 0 - for _, it := range fresh { - own += len(it) - } + kept = append(prior[:len(prior):len(prior)], kept...) + } + own := 0 + for _, it := range fresh { + own += len(it) } if ask.id == "" { // the answer note is wanted for a request naming none too @@ -289,7 +293,7 @@ func previousResponse(w http.ResponseWriter, r *http.Request, from provider.Prot for _, it := range output { own += len(it) } - t := &prevTurn{key: prevKey{scope, id}, items: append(input[:len(input):len(input)], output...), own: own} + t := &prevTurn{key: prevKey{scope, id}, items: append(kept, output...), own: own} ask.mu.Lock() if ask.answered != "" { t.upstreamID, t.route = id, ask.answered @@ -301,8 +305,8 @@ func previousResponse(w http.ResponseWriter, r *http.Request, from provider.Prot // prevInput is a Responses input as items: a string is one user message. // fresh is nil for an input that is neither. The items share the body's -// bytes, which nothing writes once read: a turn kept holds its body, not -// a copy of it. +// bytes, which nothing writes once read; what is kept past the request +// copies them (ownItems). func prevInput(raw json.RawMessage) (all, fresh []json.RawMessage) { raw = bytes.TrimSpace(raw) if len(raw) == 0 { diff --git a/internal/gateway/prev_response_test.go b/internal/gateway/prev_response_test.go index 755ad0253..b0ec79238 100644 --- a/internal/gateway/prev_response_test.go +++ b/internal/gateway/prev_response_test.go @@ -261,3 +261,34 @@ func TestPrevStoreBounded(t *testing.T) { t.Fatal("kept past its time") } } + +// TestKeptTurnOwnsItsInput: a kept response holds its input, not the body +// it was read from with the request's tools and instructions, and counts +// what it holds. +func TestKeptTurnOwnsItsInput(t *testing.T) { + old := prevResponses + prevResponses = newPrevStore(prevCap, prevTTL) + t.Cleanup(func() { prevResponses = old }) + body := []byte(`{"model":"m","instructions":"` + strings.Repeat("rules ", 1000) + `","input":[{"type":"message","role":"user","content":"hi"}]}`) + r := httptest.NewRequest("POST", "/v1/responses", nil) + r.RemoteAddr = "127.0.0.1:5555" + w, r, _, done := previousResponse(httptest.NewRecorder(), r, provider.Responses, body) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + io.WriteString(w, `{"id":"resp_1","object":"response","status":"completed","output":[{"type":"message","role":"assistant","content":"ok"}]}`) + done() + for i := range body { + body[i] = 'x' + } + kept, ok := prevResponses.get(prevKey{prevScope(r), "resp_1"}) + if !ok { + t.Fatal("nothing kept") + } + want := `[{"type":"message","role":"user","content":"hi"},{"type":"message","role":"assistant","content":"ok"}]` + if got, _ := json.Marshal(kept.items); string(got) != want { + t.Fatalf("kept after the body was written over: %s", got) + } + if n := len(want) - len("[,]"); kept.own != n { + t.Fatalf("own %d, want the %d bytes kept", kept.own, n) + } +}