Files
alan 18e7d073a6 Harden paged chat history loading
Close the per-thread history loading gate before launching requests, make live/history part reconciliation linear, and reject duplicate history envelope fields.

Complete task-owned history paging work remains preserved in the preceding recovery commit. Checked packaged templates and packaged authoring skill references: neither needs an update; docs/notes/large-chat-history.md records the stale shared documentation.
2026-10-18 15:21:09 +01:00

205 lines
7.6 KiB
Go

package opencode
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"testing"
)
func mustHistoryJSON(t *testing.T, value any) string {
t.Helper()
data, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
return string(data)
}
// A generated fixture, not a live-model history. No allocation proportional to
// the oversized string exists on either side of the allocation measurement.
type historyRepeatReader struct {
remaining int
read int
maxRead int
cancel context.CancelFunc
}
func (r *historyRepeatReader) Read(b []byte) (int, error) {
if len(b) > r.maxRead {
r.maxRead = len(b)
}
n := min(len(b), r.remaining)
for i := range b[:n] {
b[i] = 'x'
}
r.remaining -= n
r.read += n
if r.cancel != nil && r.read > 1<<20 {
r.cancel()
}
if n == 0 {
return 0, io.EOF
}
return n, nil
}
func historyLargeFixture(detail io.Reader) io.Reader {
return io.MultiReader(
strings.NewReader(`{"data":[{"id":"new","type":"assistant","content":[{"type":"tool","state":{"status":"completed","content":[{"type":"text","text":"`),
detail,
strings.NewReader(`"}]}},{"type":"text","text":"Newest answer: \uD83C\uDF19"}]}],"cursor":{"next":"opaque+/=&"}}`),
)
}
func TestHistoryStreamingAllocationBound(t *testing.T) {
for _, size := range []int{2 << 20, 32 << 20} {
t.Run(fmt.Sprint(size), func(t *testing.T) {
r := &historyRepeatReader{remaining: size}
reader := historyLargeFixture(r)
runtime.GC()
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
messages, next, _, err := decodeChatHistoryPage(context.Background(), "ses_1", reader)
runtime.ReadMemStats(&after)
allocated := after.TotalAlloc - before.TotalAlloc
t.Logf("wire detail=%d allocated=%d max read=%d", size, allocated, r.maxRead)
if err != nil {
t.Fatal(err)
}
if allocated > 1<<20 || r.maxRead > 32<<10 || r.read != size {
t.Fatalf("unbounded reader: alloc=%d read=%d chunk=%d", allocated, r.read, r.maxRead)
}
if next != "opaque+/=&" || len(messages) != 1 || messages[0].Parts[1].Text != "Newest answer: 🌙" || !messages[0].Parts[0].State.Truncated {
t.Fatalf("projection lost answer/cursor: %+v", messages)
}
})
}
}
func TestHistoryStreamingCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := &historyRepeatReader{remaining: 32 << 20, cancel: cancel}
_, _, _, err := decodeChatHistoryPage(ctx, "ses_1", historyLargeFixture(r))
if !errors.Is(err, context.Canceled) || r.read >= 2<<20 {
t.Fatalf("cancellation did not interrupt discarded string: read=%d err=%v", r.read, err)
}
}
func TestHistoryBoundsAllToolDetails(t *testing.T) {
for _, detail := range []string{
`"input":{"text":"` + strings.Repeat("x", 2<<20) + `"}`,
`"metadata":{"blob":"` + strings.Repeat("x", 2<<20) + `"}`,
`"content":[{"type":"file","data":"` + strings.Repeat("x", 2<<20) + `"}]`,
`"content":[` + strings.Repeat(`{"type":"text","text":"small"},`, 10000) + `{"type":"text","text":"last"}]`,
} {
raw := `{"data":[{"type":"assistant","content":[{"type":"tool","state":{` + detail + `}},{"type":"text","text":"answer"}]}]}`
messages, _, _, err := decodeChatHistoryPage(context.Background(), "ses_1", strings.NewReader(raw))
if err != nil || len(messages) != 1 || !messages[0].Parts[0].State.Truncated || messages[0].Parts[1].Text != "answer" {
t.Fatalf("detail prevented answer: %v", err)
}
}
}
func TestHistoryLegacyLargeToolAndNonJSON(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/session/ses_1/message" {
t.Errorf("wrong legacy path: %s", r.URL)
}
if r.URL.Query().Get("directory") == "not-json" {
io.WriteString(w, "not JSON")
return
}
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `[{"info":{"id":"msg_1","role":"assistant"},"parts":[{"type":"tool","state":{"content":[{"text":"`)
io.Copy(w, &historyRepeatReader{remaining: 2 << 20})
io.WriteString(w, `"}]}},{"type":"text","text":"legacy answer"}]}]`)
}))
defer srv.Close()
conn := &Connection{Backend: "v1", URL: srv.URL}
chat := NewChat(conn)
page, err := chat.Messages(context.Background(), "ses_1", 1, "")
if err != nil || page.HasMore || len(page.Messages) != 1 || !page.Messages[0].Parts[0].State.Truncated || page.Messages[0].Parts[1].Text != "legacy answer" {
t.Fatalf("legacy history: %+v %v", page, err)
}
conn.Directory = "not-json"
if _, err := chat.Messages(context.Background(), "ses_1", 1, ""); err == nil || !strings.Contains(err.Error(), "non-JSON") {
t.Fatalf("accepted non-JSON history: %v", err)
}
}
func TestHistoryRejectsMalformedIncludingDiscardedDetail(t *testing.T) {
for _, raw := range []string{
`null`, `{"data":null}`, `{"data":{}}`, `{"data":[],"cursor":{"next":42}}`,
`{"data":[],"data":[]}`, `{"data":[],"cursor":{"next":"one"},"cursor":{"next":"two"}}`,
`{"data":[]} {}`, `{"data":[}`, `{"data":[],}`, `{"data":[],"x":01}`, `{"data":[],"x":1,"x":2}`,
`{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20) + `\q"}}`,
`{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20) + `\u12G4"}}`,
`{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20),
strings.Repeat("[", 130) + strings.Repeat("]", 130),
} {
if _, _, _, err := decodeChatHistoryPage(context.Background(), "ses_1", strings.NewReader(raw)); err == nil {
t.Fatalf("accepted malformed fixture (length %d)", len(raw))
}
}
}
func TestHistoryDoesNotCapConversationTextOrSmallToolState(t *testing.T) {
answer := strings.Repeat("answer 🌙\n", 150000)
raw := mustHistoryJSON(t, map[string]any{"data": []any{
map[string]any{"id": "new", "type": "assistant", "text": "", "content": []any{
map[string]any{"type": "text", "text": answer},
map[string]any{"type": "tool", "state": map[string]any{"status": "completed", "input": map[string]any{"x": "y"}, "metadata": map[string]any{"exit": 0}}},
}},
}})
messages, _, _, err := decodeChatHistoryPage(context.Background(), "ses_1", strings.NewReader(raw))
if err != nil || len(messages) != 1 || messages[0].Parts[0].Text != answer {
t.Fatalf("conversation text capped: %v", err)
}
state := messages[0].Parts[1].State
if state.Truncated || state.Status != "completed" || string(state.Input) != `{"x":"y"}` || state.Metadata["exit"] != float64(0) {
t.Fatalf("small state changed: %+v", state)
}
}
func TestHistoryHTTPAuthenticationAndCancellation(t *testing.T) {
started := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, _ := r.BasicAuth()
if user != "operator" || pass != "fixture-password" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if r.URL.Query().Get("directory") != "/fixture dir" {
t.Errorf("missing directory scope: %s", r.URL)
}
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"data":[`)
w.(http.Flusher).Flush()
close(started)
<-r.Context().Done()
}))
defer srv.Close()
conn := &Connection{Backend: "v2", URL: srv.URL, username: "operator", Directory: "/fixture dir"}
chat := NewChat(conn)
if _, err := chat.Messages(context.Background(), "ses_1", 1, ""); err == nil || !conn.unauthorized || conn.LastHTTPStatus != 401 {
t.Fatalf("auth refusal not recorded: %v %+v", err, conn)
}
conn.password = "fixture-password"
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() { _, err := chat.Messages(ctx, "ses_1", 1, ""); done <- err }()
<-started
cancel()
if err := <-done; !errors.Is(err, context.Canceled) {
t.Fatalf("HTTP cancellation = %v", err)
}
}