gateway: a Codex socket's held reply keeps its input, not the frame it came in
A held turn's fresh input items were views into the response.create frame they were decoded from, so each held reply kept that whole frame alive: Codex's tools and instructions with it, for as long as the socket held the reply (~39 MB of a long session's live heap). keep now copies the fresh items into one buffer of their own; the conversation held before is already owned, and the reply's items were already copied. Run on mote: go vet ./internal/gateway; go test -run 'TestHeldTurnOwnsItsInput|TestResponsesSocket|TestCodexSocket' ./internal/gateway ok. Broken by hand (keep appends fresh as views): TestHeldTurnOwnsItsInput fails, held items read back as the overwritten frame. Not run: the full gateway package before the push, and a real Codex session over the socket.
This commit is contained in:
@@ -399,13 +399,37 @@ func (rs *responsesSocket) held(id string) ([]json.RawMessage, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (rs *responsesSocket) keep(t heldTurn) {
|
||||
// keep holds the reply id ends: the conversation before this turn (held
|
||||
// already), the turn's own input and the reply's items. The input is
|
||||
// copied out of the frame it was read from: a view kept it whole, with
|
||||
// Codex's tools and instructions, on every turn a socket held, ~39 MB of
|
||||
// a long session's live heap.
|
||||
func (rs *responsesSocket) keep(id string, prior, fresh, reply []json.RawMessage) {
|
||||
t := heldTurn{id: id, items: make([]json.RawMessage, 0, len(prior)+len(fresh)+len(reply))}
|
||||
t.items = append(t.items, prior...)
|
||||
t.items = append(t.items, ownItems(fresh)...)
|
||||
t.items = append(t.items, reply...)
|
||||
rs.turns = append(rs.turns, t)
|
||||
if len(rs.turns) > socketKeep {
|
||||
rs.turns = append(rs.turns[:0:0], rs.turns[len(rs.turns)-socketKeep:]...)
|
||||
}
|
||||
}
|
||||
|
||||
// ownItems are items copied into one buffer of their own.
|
||||
func ownItems(items []json.RawMessage) []json.RawMessage {
|
||||
n := 0
|
||||
for _, it := range items {
|
||||
n += len(it)
|
||||
}
|
||||
buf := make([]byte, 0, n)
|
||||
out := make([]json.RawMessage, len(items))
|
||||
for i, it := range items {
|
||||
buf = append(buf, it...)
|
||||
out[i] = buf[len(buf)-len(it) : len(buf) : len(buf)]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// turn serves one response.create.
|
||||
func (rs *responsesSocket) turn(ctx context.Context, t *socketTurnRun, frame []byte) {
|
||||
rs.frames++
|
||||
@@ -421,6 +445,7 @@ func (rs *responsesSocket) turn(ctx context.Context, t *socketTurnRun, frame []b
|
||||
return
|
||||
}
|
||||
input := fresh
|
||||
var held []json.RawMessage
|
||||
if raw := q["previous_response_id"]; len(raw) > 0 && !bytes.Equal(raw, []byte("null")) {
|
||||
var id string
|
||||
json.Unmarshal(raw, &id)
|
||||
@@ -431,7 +456,7 @@ func (rs *responsesSocket) turn(ctx context.Context, t *socketTurnRun, frame []b
|
||||
"Previous response with id '"+id+"' not found.", "previous_response_id")
|
||||
return
|
||||
}
|
||||
input = append(prior[:len(prior):len(prior)], fresh...)
|
||||
held, input = prior, append(prior[:len(prior):len(prior)], fresh...)
|
||||
rs.continued++
|
||||
}
|
||||
// a warmup (generate false) asks for no reply: Codex sends it with
|
||||
@@ -454,7 +479,7 @@ func (rs *responsesSocket) turn(ctx context.Context, t *socketTurnRun, frame []b
|
||||
resp["usage"] = map[string]any{"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
|
||||
b, _ = json.Marshal(map[string]any{"type": "response.completed", "response": resp})
|
||||
if rs.write(ctx, b) == nil {
|
||||
rs.keep(heldTurn{id: id, items: input})
|
||||
rs.keep(id, held, fresh, nil)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -480,7 +505,7 @@ func (rs *responsesSocket) turn(ctx context.Context, t *socketTurnRun, frame []b
|
||||
return
|
||||
}
|
||||
if out.id != "" && out.completed {
|
||||
rs.keep(heldTurn{id: out.id, items: append(input[:len(input):len(input)], out.items...)})
|
||||
rs.keep(out.id, held, fresh, out.items)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -672,3 +673,28 @@ func TestCodexSocketOtherPaths(t *testing.T) {
|
||||
t.Errorf("%d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHeldTurnOwnsItsInput: a held reply's input was a view into the
|
||||
// frame it was read from, which kept the whole frame (Codex's tools and
|
||||
// instructions too) for as long as the socket held the reply. Written
|
||||
// over, the frame leaves what is held as it was.
|
||||
func TestHeldTurnOwnsItsInput(t *testing.T) {
|
||||
frame := []byte(`{"input":[{"type":"message","role":"user","content":"hi"},{"type":"message","role":"user","content":"again"}],"tools":["x"]}`)
|
||||
var q map[string]json.RawMessage
|
||||
if err := decodeJSONView(frame, &q); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, fresh := prevInput(q["input"])
|
||||
prior := []json.RawMessage{json.RawMessage(`{"type":"message","role":"user","content":"before"}`)}
|
||||
reply := []json.RawMessage{json.RawMessage(`{"type":"message","role":"assistant","content":"ok"}`)}
|
||||
rs := &responsesSocket{}
|
||||
rs.keep("resp_1", prior, fresh, reply)
|
||||
want := `[{"type":"message","role":"user","content":"before"},{"type":"message","role":"user","content":"hi"},{"type":"message","role":"user","content":"again"},{"type":"message","role":"assistant","content":"ok"}]`
|
||||
for i := range frame {
|
||||
frame[i] = 'x'
|
||||
}
|
||||
items, ok := rs.held("resp_1")
|
||||
if got, _ := json.Marshal(items); !ok || string(got) != want {
|
||||
t.Fatalf("held after the frame was written over: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user