WIP preserve large chat history recovery
This commit is contained in:
+66
-14
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -25,8 +26,9 @@ var ErrPermissionUnsupported = errors.New("OpenCode permission approval is not i
|
||||
// Chat is the brokered OpenCode chat adapter (route B in the research): the
|
||||
// Pipelines server owns the OpenCode endpoint and credentials and turns the v2
|
||||
// HTTP API into protocol.ChatEventFrame frames. It reuses the owned-endpoint
|
||||
// semantics, the session-id shape and the 64 KiB / 1 MiB bounds of Connection so
|
||||
// no second transport or credential path is introduced.
|
||||
// semantics and session-id shape of Connection, with its normal 64 KiB / 1 MiB
|
||||
// call guards. History alone streams/project tool details before decoding; it
|
||||
// does not introduce a total-history cap or a second credential path.
|
||||
type Chat struct {
|
||||
conn *Connection
|
||||
// mu serializes the adapter's HTTP calls. Connection records its last
|
||||
@@ -90,27 +92,63 @@ func (c *Chat) Interrupt(ctx context.Context, id string) error {
|
||||
// reuses Connection.Messages, including its read-only OutputFormat readback
|
||||
// fallback, instead of duplicating that path. It maps the verified assistant
|
||||
// content parts (text | reasoning | tool) onto protocol.ChatMessage.
|
||||
func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.ChatMessage, error) {
|
||||
func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
|
||||
if !sessionID.MatchString(id) {
|
||||
return nil, fmt.Errorf("invalid OpenCode session id")
|
||||
return protocol.ChatHistoryPage{}, fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
var data []byte
|
||||
var err error
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var reader io.Reader
|
||||
if c.conn.Backend == "v1" {
|
||||
data, err = c.conn.Messages(ctx, id)
|
||||
} else {
|
||||
path := "/api/session/" + id + "/message"
|
||||
if limit > 0 {
|
||||
path += fmt.Sprintf("?limit=%d", limit)
|
||||
var body io.ReadCloser
|
||||
body, err = c.conn.HistoryNative(ctx, "/session/"+id+"/message")
|
||||
if err == nil {
|
||||
defer body.Close()
|
||||
reader = body
|
||||
} else if c.conn.LastHTTPStatus == 400 {
|
||||
// Preserve the existing OutputFormat/read-only export recovery on
|
||||
// native serve. Ordinary history is streamed, never Call-buffered.
|
||||
data, err = c.conn.Messages(ctx, id)
|
||||
reader = bytes.NewReader(data)
|
||||
}
|
||||
} else {
|
||||
if limit <= 0 {
|
||||
limit = 100 // bounded newest-page request, including legacy clients
|
||||
}
|
||||
path := "/api/session/" + id + "/message"
|
||||
values := make([]string, 0, 2)
|
||||
if limit > 0 {
|
||||
values = append(values, fmt.Sprintf("limit=%d", limit))
|
||||
}
|
||||
if cursor != "" {
|
||||
values = append(values, "cursor="+url.QueryEscape(cursor))
|
||||
}
|
||||
if len(values) > 0 {
|
||||
path += "?" + strings.Join(values, "&")
|
||||
}
|
||||
var body io.ReadCloser
|
||||
body, err = c.conn.HistoryNative(ctx, path)
|
||||
if err == nil {
|
||||
defer body.Close()
|
||||
reader = body
|
||||
}
|
||||
data, err = c.conn.Call(ctx, "get", path, nil)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return protocol.ChatHistoryPage{}, err
|
||||
}
|
||||
return decodeChatMessages(id, data)
|
||||
messages, next, descending, err := decodeChatHistoryPage(ctx, id, reader)
|
||||
if err != nil {
|
||||
return protocol.ChatHistoryPage{}, err
|
||||
}
|
||||
// The verified API returns newest first. The transcript is chronological.
|
||||
if descending {
|
||||
for left, right := 0, len(messages)-1; left < right; left, right = left+1, right-1 {
|
||||
messages[left], messages[right] = messages[right], messages[left]
|
||||
}
|
||||
}
|
||||
return protocol.ChatHistoryPage{Messages: messages, NextCursor: next, HasMore: next != ""}, nil
|
||||
}
|
||||
|
||||
// Sessions lists the OpenCode sessions on the endpoint. On the V2 HTTP API that
|
||||
@@ -793,6 +831,10 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapChatMessages(sessionID, records), nil
|
||||
}
|
||||
|
||||
func mapChatMessages(sessionID string, records []nativeChatMessage) []protocol.ChatMessage {
|
||||
messages := make([]protocol.ChatMessage, 0, len(records))
|
||||
for _, record := range records {
|
||||
role := record.role()
|
||||
@@ -815,9 +857,19 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage,
|
||||
}
|
||||
messages = append(messages, out)
|
||||
}
|
||||
return messages, nil
|
||||
return messages
|
||||
}
|
||||
|
||||
// decodeChatHistoryPage reads the V2 cursor envelope while retaining the older
|
||||
// bare-array decoder for native serve. Tool output is intentionally previewed:
|
||||
// one command's unbounded stdout must not make the entire transcript or its final
|
||||
// answer unavailable over the websocket.
|
||||
func decodeChatHistoryPage(ctx context.Context, sessionID string, reader io.Reader) ([]protocol.ChatMessage, string, bool, error) {
|
||||
return readHistoryPage(ctx, sessionID, reader)
|
||||
}
|
||||
|
||||
const historyToolPreviewBytes = 64 << 10
|
||||
|
||||
// nativeChatSession is one listed session in the verified V2 shape
|
||||
// ({id,title,agent,model,cost,tokens,outcome,time,title,location.directory,
|
||||
// parentID,permissions}); only the fields the protocol surfaces are read. The
|
||||
|
||||
@@ -161,6 +161,9 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) {
|
||||
if err := chat.Interrupt(ctx, "bad id"); err == nil {
|
||||
t.Fatal("invalid interrupt session id must be rejected")
|
||||
}
|
||||
if _, err := chat.Messages(ctx, "bad id", 1, ""); err == nil {
|
||||
t.Fatal("invalid history session id must be rejected")
|
||||
}
|
||||
if _, err := chat.Permissions(ctx, "not-a-session"); err == nil {
|
||||
t.Fatal("invalid permission session id must be rejected")
|
||||
}
|
||||
@@ -498,6 +501,44 @@ func TestDecodeChatMessagesSkipsUnknownPartsAndRoles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatMessagesPagesLargeToolHistoryWithoutGlobalResponseLimit(t *testing.T) {
|
||||
large := strings.Repeat("x", MaxResponse+1)
|
||||
var paths []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
paths = append(paths, r.URL.String())
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.URL.Query().Get("cursor") == "older-token" {
|
||||
_, _ = io.WriteString(w, `{"data":[{"id":"old","type":"user","time":{"created":1},"text":"older"}],"cursor":{"next":null}}`)
|
||||
return
|
||||
}
|
||||
_, _ = io.WriteString(w, `{"data":[{"id":"new","type":"assistant","time":{"created":2},"content":[{"type":"tool","id":"tool","state":{"status":"completed","content":[{"type":"text","text":"`+large+`"}]}},{"type":"text","text":"final answer"}]}],"cursor":{"next":"older-token"}}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
conn := &Connection{Backend: "v2", URL: srv.URL}
|
||||
chat := NewChat(conn)
|
||||
page, err := chat.Messages(context.Background(), "ses_1", 1, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !page.HasMore || page.NextCursor != "older-token" || len(page.Messages) != 1 {
|
||||
t.Fatalf("page = %+v", page)
|
||||
}
|
||||
parts := page.Messages[0].Parts
|
||||
if len(parts) != 2 || parts[0].State == nil || !parts[0].State.Truncated || parts[1].Text != "final answer" {
|
||||
t.Fatalf("parts = %+v", parts)
|
||||
}
|
||||
older, err := chat.Messages(context.Background(), "ses_1", 1, page.NextCursor)
|
||||
if err != nil || older.HasMore || len(older.Messages) != 1 || older.Messages[0].ID != "old" {
|
||||
t.Fatalf("older = %+v, %v", older, err)
|
||||
}
|
||||
if len(paths) != 2 || !strings.Contains(paths[1], "cursor=older-token") {
|
||||
t.Fatalf("requests = %v", paths)
|
||||
}
|
||||
if _, err := conn.Call(context.Background(), "get", "/api/session/ses_1/message", nil); err == nil || !strings.Contains(err.Error(), "1 MiB") {
|
||||
t.Fatalf("generic Call safeguard changed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatSessionsListsAndLiftsShape loads the verified V2 session list
|
||||
// (GET /api/session: {"data":[{id,...,location.directory,time{created,updated},
|
||||
// parentID}]}) and asserts the adapter reads it exactly once, lifts the nested
|
||||
@@ -772,11 +813,11 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) {
|
||||
if len(got) != 1 || got[0].Kind != protocol.ChatEventText || got[0].MessageID != "msg_1" {
|
||||
t.Fatalf("stream frames = %+v", got)
|
||||
}
|
||||
messages, err := chat.Messages(ctx, "ses_1", 0)
|
||||
page, err := chat.Messages(ctx, "ses_1", 0, "")
|
||||
if err != nil {
|
||||
t.Fatalf("messages: %v", err)
|
||||
}
|
||||
if !sawMessages || len(messages) != 1 || messages[0].ID != "msg_1" {
|
||||
t.Fatalf("messages = %+v", messages)
|
||||
if !sawMessages || len(page.Messages) != 1 || page.Messages[0].ID != "msg_1" {
|
||||
t.Fatalf("messages = %+v", page.Messages)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,6 +321,51 @@ func (c *Connection) Native(ctx context.Context, method, path string, body any)
|
||||
return c.nativeRequest(ctx, method, path, body, c.Directory)
|
||||
}
|
||||
|
||||
// HistoryNative performs an authenticated history read without Connection.Call's
|
||||
// general 1 MiB response guard. Chat history is paged and projected before it is
|
||||
// sent to a client; ordinary API calls keep their existing safeguard.
|
||||
func (c *Connection) HistoryNative(ctx context.Context, path string) (io.ReadCloser, error) {
|
||||
c.LastHTTPStatus = 0
|
||||
c.LastHTTPOperation = "GET " + path
|
||||
u, err := url.Parse(c.URL + path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.Directory != "" {
|
||||
query := u.Query()
|
||||
query.Set("directory", c.Directory)
|
||||
u.RawQuery = query.Encode()
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.SetBasicAuth(c.serverUser(), c.password)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return nil, fmt.Errorf("OpenCode history request failed")
|
||||
}
|
||||
c.LastHTTPStatus = resp.StatusCode
|
||||
c.LastHTTPOperation = "GET " + path
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
c.unauthorized = true
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
defer resp.Body.Close()
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 16<<10))
|
||||
return nil, fmt.Errorf("OpenCode GET %s failed (HTTP %d)", path, resp.StatusCode)
|
||||
}
|
||||
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
|
||||
resp.Body.Close()
|
||||
return nil, fmt.Errorf("OpenCode GET %s returned a non-JSON API response", path)
|
||||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
// nativeRequest is the shared Native request path. An empty directory omits the
|
||||
// ?directory= query parameter; the V2 session list then returns sessions across
|
||||
// every directory instead of only the connection's own.
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// historyJSON projects tool state BEFORE encoding/json sees it. Decoder.Token
|
||||
// alone is not sufficient: it allocates an entire JSON string, even when the
|
||||
// caller immediately discards it. This scanner consumes strings byte by byte
|
||||
// through a fixed 32 KiB reader, including escapes and discarded detail.
|
||||
//
|
||||
// Workspace is O(32 KiB + 64 KiB + depth*256), depth <= 128. Retained memory is
|
||||
// O(returned messages + largest projected message): answer text is never capped; each tool state
|
||||
// contributes at most 64 KiB. An oversized state (including input, metadata,
|
||||
// binary content or many small fields) is replaced by an honest omission card,
|
||||
// NOT a raw JSON prefix. Neither wire size nor a single string size is a cap on
|
||||
// history. No temporary disk copy or unbounded raw message/page is retained.
|
||||
type historyJSON struct {
|
||||
ctx context.Context
|
||||
r *bufio.Reader
|
||||
}
|
||||
|
||||
// Negative limits are used only for a projected message (output-proportional).
|
||||
// State, cursor and key captures reuse the existing bounded output helper.
|
||||
type historyBuffer = boundedOutput
|
||||
|
||||
func (b *boundedOutput) put(c byte) {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
b.WriteByte(c)
|
||||
}
|
||||
|
||||
func readHistoryPage(ctx context.Context, sessionID string, r io.Reader) ([]protocol.ChatMessage, string, bool, error) {
|
||||
p := historyJSON{ctx: ctx, r: bufio.NewReaderSize(r, 32<<10)}
|
||||
messages := make([]protocol.ChatMessage, 0)
|
||||
var next string
|
||||
readMessages := func() error {
|
||||
if err := p.take(nil, '['); err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := p.peek()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c == ']' {
|
||||
return p.take(nil, ']')
|
||||
}
|
||||
for {
|
||||
out := &historyBuffer{limit: -1}
|
||||
if err := p.value(out, 1, true); err != nil {
|
||||
return err
|
||||
}
|
||||
var record nativeChatMessage
|
||||
if err := json.Unmarshal(out.Bytes(), &record); err != nil {
|
||||
return err
|
||||
}
|
||||
messages = append(messages, mapChatMessages(sessionID, []nativeChatMessage{record})...)
|
||||
c, err := p.peek()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c == ']' {
|
||||
return p.take(nil, ']')
|
||||
}
|
||||
if err := p.take(nil, ','); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
c, err := p.peek()
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
descending := c == '{'
|
||||
if c == '[' {
|
||||
err = readMessages()
|
||||
} else if descending {
|
||||
p.take(nil, '{')
|
||||
found := false
|
||||
for err == nil {
|
||||
key := &historyBuffer{limit: 256}
|
||||
if err = p.str(key, nil); err != nil {
|
||||
break
|
||||
}
|
||||
var name string
|
||||
if !key.overflow {
|
||||
_ = json.Unmarshal(key.Bytes(), &name)
|
||||
}
|
||||
if err = p.take(nil, ':'); err != nil {
|
||||
break
|
||||
}
|
||||
switch name {
|
||||
case "data":
|
||||
if found {
|
||||
err = fmt.Errorf("duplicate history data")
|
||||
} else {
|
||||
found = true
|
||||
err = readMessages()
|
||||
}
|
||||
case "cursor":
|
||||
cursor := &historyBuffer{limit: 64 << 10}
|
||||
err = p.value(cursor, 1, false)
|
||||
if err == nil {
|
||||
var decoded struct {
|
||||
Next string `json:"next"`
|
||||
}
|
||||
if cursor.overflow {
|
||||
err = fmt.Errorf("history cursor exceeds 64 KiB")
|
||||
} else {
|
||||
err = json.Unmarshal(cursor.Bytes(), &decoded)
|
||||
next = decoded.Next
|
||||
}
|
||||
}
|
||||
default:
|
||||
err = p.value(nil, 1, false)
|
||||
}
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
c, err = p.peek()
|
||||
if err != nil || c == '}' {
|
||||
if err == nil {
|
||||
err = p.take(nil, '}')
|
||||
}
|
||||
break
|
||||
}
|
||||
err = p.take(nil, ',')
|
||||
}
|
||||
if err == nil && !found {
|
||||
err = fmt.Errorf("missing history data")
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("invalid history envelope")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, "", false, fmt.Errorf("invalid OpenCode history: %w", err)
|
||||
}
|
||||
if _, err := p.peek(); err != io.EOF {
|
||||
if err == nil {
|
||||
err = fmt.Errorf("trailing JSON data")
|
||||
}
|
||||
return nil, "", false, err
|
||||
}
|
||||
return messages, next, descending, nil
|
||||
}
|
||||
|
||||
func (p *historyJSON) peek() (byte, error) {
|
||||
for {
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
b, err := p.r.Peek(1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
switch b[0] {
|
||||
case ' ', '\n', '\r', '\t':
|
||||
p.r.ReadByte()
|
||||
default:
|
||||
return b[0], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *historyJSON) take(out *historyBuffer, want byte) error {
|
||||
c, err := p.peek()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c != want {
|
||||
return fmt.Errorf("expected JSON delimiter")
|
||||
}
|
||||
p.r.ReadByte()
|
||||
out.put(c)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *historyJSON) str(out, key *historyBuffer) error {
|
||||
if err := p.take(out, '"'); err != nil {
|
||||
return err
|
||||
}
|
||||
key.put('"')
|
||||
escape, unicode := false, 0
|
||||
for {
|
||||
// Check cancellation even within a multi-gigabyte discarded string.
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := p.r.ReadByte()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out.put(c)
|
||||
key.put(c)
|
||||
if unicode > 0 {
|
||||
if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F') {
|
||||
return fmt.Errorf("invalid Unicode escape")
|
||||
}
|
||||
unicode--
|
||||
} else if escape {
|
||||
escape = false
|
||||
switch c {
|
||||
case 'u':
|
||||
unicode = 4
|
||||
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
|
||||
default:
|
||||
return fmt.Errorf("invalid string escape")
|
||||
}
|
||||
} else {
|
||||
switch {
|
||||
case c == '"':
|
||||
return nil
|
||||
case c == '\\':
|
||||
escape = true
|
||||
case c < 0x20:
|
||||
return fmt.Errorf("invalid string control character")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *historyJSON) value(out *historyBuffer, depth int, project bool) error {
|
||||
if depth > 128 {
|
||||
return fmt.Errorf("history JSON nesting exceeds 128")
|
||||
}
|
||||
c, err := p.peek()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch c {
|
||||
case '"':
|
||||
return p.str(out, nil)
|
||||
case '{', '[':
|
||||
open, close := c, byte('}')
|
||||
if open == '[' {
|
||||
close = ']'
|
||||
}
|
||||
p.take(out, open)
|
||||
c, err = p.peek()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c == close {
|
||||
return p.take(out, close)
|
||||
}
|
||||
for {
|
||||
var name string
|
||||
if open == '{' {
|
||||
key := &historyBuffer{limit: 256}
|
||||
if err := p.str(out, key); err != nil {
|
||||
return err
|
||||
}
|
||||
if !key.overflow {
|
||||
_ = json.Unmarshal(key.Bytes(), &name)
|
||||
}
|
||||
if err := p.take(out, ':'); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if project && name == "state" {
|
||||
state := &historyBuffer{limit: historyToolPreviewBytes}
|
||||
if err := p.value(state, depth+1, false); err != nil {
|
||||
return err
|
||||
}
|
||||
data := state.Bytes()
|
||||
if state.overflow {
|
||||
data = []byte(`{"truncated":true,"content":[{"type":"text","text":"Tool details omitted: stored result exceeds the 64 KiB history preview budget."}]}`)
|
||||
}
|
||||
for _, b := range data {
|
||||
out.put(b)
|
||||
}
|
||||
} else if err := p.value(out, depth+1, project); err != nil {
|
||||
return err
|
||||
}
|
||||
c, err = p.peek()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c == close {
|
||||
return p.take(out, close)
|
||||
}
|
||||
if err := p.take(out, ','); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
default:
|
||||
// JSON numbers/literals are small in this schema. Validate without ever
|
||||
// accumulating an attacker-controlled unbounded primitive.
|
||||
token := &historyBuffer{limit: 128}
|
||||
for {
|
||||
b, err := p.r.Peek(1)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c = b[0]
|
||||
if c == ',' || c == ']' || c == '}' || c == ' ' || c == '\n' || c == '\r' || c == '\t' {
|
||||
break
|
||||
}
|
||||
p.r.ReadByte()
|
||||
token.put(c)
|
||||
if token.overflow {
|
||||
return fmt.Errorf("invalid history primitive")
|
||||
}
|
||||
}
|
||||
if !json.Valid(token.Bytes()) {
|
||||
return fmt.Errorf("invalid history primitive")
|
||||
}
|
||||
for _, b := range token.Bytes() {
|
||||
out.put(b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
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":[],}`, `{"data":[],"x":01}`,
|
||||
`{"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)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,9 @@ type boundedOutput struct {
|
||||
}
|
||||
|
||||
func (b *boundedOutput) Write(p []byte) (int, error) {
|
||||
if b.limit < 0 {
|
||||
return b.Buffer.Write(p)
|
||||
}
|
||||
n := len(p)
|
||||
remaining := b.limit - b.Len()
|
||||
if n > remaining {
|
||||
@@ -26,6 +29,16 @@ func (b *boundedOutput) Write(p []byte) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// WriteByte keeps byte-oriented history scanning under the same bound as Write;
|
||||
// the embedded Buffer's promoted WriteByte would otherwise bypass the cap.
|
||||
func (b *boundedOutput) WriteByte(c byte) error {
|
||||
if b.limit >= 0 && b.Len() >= b.limit {
|
||||
b.overflow = true
|
||||
return nil
|
||||
}
|
||||
return b.Buffer.WriteByte(c)
|
||||
}
|
||||
|
||||
// ReadFrom reads through the bounded Write so the cap applies even when
|
||||
// os/exec's io.Copy selects a ReadFrom on the destination: without this the
|
||||
// embedded bytes.Buffer's promoted ReadFrom would bypass Write and buffer the
|
||||
|
||||
Reference in New Issue
Block a user