Merge branch 'main' into manager-skill-bootstrap

This commit is contained in:
2026-10-18 15:23:27 +01:00
21 changed files with 1115 additions and 76 deletions
+66 -14
View File
@@ -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
shellMu sync.Mutex
@@ -92,27 +94,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
@@ -994,6 +1032,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()
@@ -1016,9 +1058,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
+44 -3
View File
@@ -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)
}
}
+45
View File
@@ -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.
+326
View File
@@ -0,0 +1,326 @@
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
fields := make(map[string]struct{})
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) != nil {
err = fmt.Errorf("invalid history envelope field")
break
}
if _, duplicate := fields[name]; duplicate {
err = fmt.Errorf("duplicate history envelope field %q", name)
break
}
fields[name] = struct{}{}
if err = p.take(nil, ':'); err != nil {
break
}
switch name {
case "data":
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
}
}
+204
View File
@@ -0,0 +1,204 @@
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)
}
}
+13
View File
@@ -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
+15
View File
@@ -193,6 +193,18 @@ type ChatAdoptRequest struct {
type ChatQuery struct {
SessionID string `json:"session_id"`
Limit int `json:"limit,omitempty"`
// Cursor is the opaque continuation returned by OpenCode's descending
// message listing. It is deliberately not interpreted by the broker.
Cursor string `json:"cursor,omitempty"`
}
// ChatHistoryPage describes the bounded slice returned by chat_messages. The
// newest page has an empty Cursor request; NextCursor remains opaque and is
// supplied unchanged to fetch older turns.
type ChatHistoryPage struct {
Messages []ChatMessage `json:"messages"`
NextCursor string `json:"next_cursor,omitempty"`
HasMore bool `json:"has_more,omitempty"`
}
// ChatPromptRequest is the chat_prompt request body: the text to send, with an
@@ -266,6 +278,9 @@ type ChatToolState struct {
Content []ChatToolContent `json:"content,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Error *ChatError `json:"error,omitempty"`
// Truncated says Content is a bounded preview of a larger stored result.
// The turn remains usable; callers must not present the preview as complete.
Truncated bool `json:"truncated,omitempty"`
}
// ChatToolContent is one returned tool result: either text or a file/uri pair
+1
View File
@@ -169,6 +169,7 @@ type Message struct {
ChatQuery *ChatQuery `json:"chat_query,omitempty"`
ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"`
ChatMessages []ChatMessage `json:"chat_messages,omitempty"`
ChatHistory *ChatHistoryPage `json:"chat_history,omitempty"`
ChatEvent *ChatEventFrame `json:"chat_event,omitempty"`
ChatPermission *ChatPermissionFrame `json:"chat_permission,omitempty"`
ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"`
+1 -1
View File
@@ -19,7 +19,7 @@ type chatBroker interface {
// can order the prompt after the subscription.
Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error
Interrupt(ctx context.Context, sessionID string) error
Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error)
Messages(ctx context.Context, sessionID string, limit int, cursor string) (protocol.ChatHistoryPage, error)
// Sessions lists the endpoint's OpenCode sessions so the handler can
// restrict them to the operator's own recorded sessions.
Sessions(ctx context.Context) ([]protocol.ChatSession, error)
+5 -9
View File
@@ -15,23 +15,19 @@ func handleChatMessages(ctx context.Context, request protocol.Message, broker ch
return
}
response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID}
// Resume from retained server state first: a reconnecting client gets the
// accumulated turns with the same stable ids without re-reading OpenCode.
if messages := hub.snapshot(query.SessionID); len(messages) > 0 {
response.ChatMessages = messages
_ = write(response)
return
}
// A hub snapshot is only the live tail. It is never authoritative history:
// serving it here used to make older turns unreachable after a reconnect.
if broker == nil {
_ = write(response)
return
}
messages, err := broker.Messages(ctx, query.SessionID, query.Limit)
page, err := broker.Messages(ctx, query.SessionID, query.Limit, query.Cursor)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
response.ChatMessages = messages
response.ChatMessages = page.Messages // legacy peers keep working.
response.ChatHistory = &page
_ = write(response)
}
+107
View File
@@ -0,0 +1,107 @@
package server
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Scratch HTTP + real /connect websocket fixture, NOT a live-model/production
// claim. Covers both >1 MiB total (many tools) and one >1 MiB tool string.
func TestChatHistoryLargeFixtureWebsocketTraversalAndReconnect(t *testing.T) {
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/session/ses_1/message" || r.URL.Query().Get("limit") != "2" {
t.Errorf("unexpected history query %s", r.URL)
}
w.Header().Set("Content-Type", "application/json")
page := 0
switch r.URL.Query().Get("cursor") {
case "":
case "older+/=&":
page = 1
case "last":
page = 2
default:
t.Errorf("cursor not passed opaquely: %s", r.URL)
}
io.WriteString(w, `{"data":[`)
for i := 0; i < 2; i++ {
if i != 0 {
io.WriteString(w, ",")
}
id := 6 - page*2 - i
// Equal timestamps deliberately: order comes from the verified V2
// descending sequence, not a guessed timestamp comparison.
fmt.Fprintf(w, `{"id":"msg_%d","type":"assistant","time":{"created":1},"content":[`, id)
if page == 0 && i == 0 {
io.WriteString(w, `{"type":"tool","id":"huge","state":{"content":[{"type":"text","text":"`)
chunk := strings.Repeat("x", 32<<10)
for n := 0; n < 65; n++ {
io.WriteString(w, chunk)
}
io.WriteString(w, `"}]}},`)
}
if page == 1 && i == 0 {
for n := 0; n < 25; n++ {
fmt.Fprintf(w, `{"type":"tool","id":"tool_%d","state":{"status":"completed","content":[{"type":"text","text":%q}]}},`, n, strings.Repeat("y", 48<<10))
}
}
fmt.Fprintf(w, `{"type":"text","text":"answer %d"}]}`, id)
}
next := []any{"older+/=&", "last", nil}[page]
encoded, _ := json.Marshal(next)
fmt.Fprintf(w, `],"cursor":{"next":%s}}`, encoded)
}))
defer fixture.Close()
broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL})
ctx, _, srv, conns := dialChatServer(t, Options{Chat: broker}, 1)
conn := conns[0]
conn.SetReadLimit(8 << 20)
cursor := ""
var ids []string
seen := map[string]bool{}
for page := 0; page < 3; page++ {
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "history", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1", Limit: 2, Cursor: cursor}})
response := readUntil(t, ctx, conn, protocol.ChatMessages)
if response.ChatHistory == nil || len(response.ChatHistory.Messages) != 2 {
t.Fatalf("missing page: %+v", response)
}
history := response.ChatHistory
if !reflect.DeepEqual(response.ChatMessages, history.Messages) {
t.Fatal("legacy/page views differ")
}
for _, message := range history.Messages {
if seen[message.ID] {
t.Fatalf("duplicate %s", message.ID)
}
seen[message.ID] = true
}
pageIDs := []string{history.Messages[0].ID, history.Messages[1].ID}
ids = append(pageIDs, ids...)
if page == 0 {
newest := history.Messages[1]
if newest.Parts[len(newest.Parts)-1].Text != "answer 6" || !newest.Parts[0].State.Truncated {
t.Fatal("huge tool prevented newest answer")
}
}
cursor = history.NextCursor
if history.HasMore != (page < 2) {
t.Fatal("incorrect continuation")
}
// Reconnect between pages: continuation is server-owned, not a hub tail.
conn.CloseNow()
conn = dialChatConn(t, ctx, srv)
conn.SetReadLimit(8 << 20)
}
if cursor != "" || !reflect.DeepEqual(ids, []string{"msg_1", "msg_2", "msg_3", "msg_4", "msg_5", "msg_6"}) {
t.Fatalf("incomplete traversal: %v cursor=%q", ids, cursor)
}
}
+12 -14
View File
@@ -69,8 +69,8 @@ func (c *channelChat) Stream(ctx context.Context, _ string, ready func(), emit f
func (c *channelChat) Interrupt(context.Context, string) error { return nil }
func (c *channelChat) Messages(context.Context, string, int) ([]protocol.ChatMessage, error) {
return nil, nil
func (c *channelChat) Messages(context.Context, string, int, string) (protocol.ChatHistoryPage, error) {
return protocol.ChatHistoryPage{}, nil
}
func (c *channelChat) Sessions(context.Context) ([]protocol.ChatSession, error) { return nil, nil }
@@ -451,10 +451,11 @@ func TestChatPendingApprovalsAllReplayed(t *testing.T) {
}
// TestChatResyncAfterReconnect is the resync acceptance test: a client that drops
// and reconnects re-syncs the whole conversation from retained state, including
// the user turn and who sent it.
// and reconnects reads authoritative history rather than mistaking a retained
// live tail for the whole conversation.
func TestChatResyncAfterReconnect(t *testing.T) {
broker := scriptedChat()
broker.messages = seededChatMessages()
ctx, _, srv, conns := dialChatServer(t, Options{Chat: broker}, 1)
phone := conns[0]
@@ -465,19 +466,16 @@ func TestChatResyncAfterReconnect(t *testing.T) {
// The phone's socket drops.
phone.CloseNow()
// A fresh connection re-syncs from retained state.
// A fresh connection reads the authoritative history rather than treating the
// retained live tail as a complete transcript.
reconnected := dialChatConn(t, ctx, srv)
writeChat(t, ctx, reconnected, protocol.Message{Type: protocol.ChatMessages, ID: "m1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}})
response := readUntil(t, ctx, reconnected, protocol.ChatMessages)
if len(response.ChatMessages) != 2 {
t.Fatalf("resync returned %d messages, want user+assistant: %+v", len(response.ChatMessages), response.ChatMessages)
if response.ChatHistory == nil {
t.Fatalf("resync did not return a history page: %+v", response)
}
user := response.ChatMessages[0]
if user.Role != protocol.ChatRoleUser || user.Client != "Pixel 9" || len(user.Parts) != 1 || user.Parts[0].Text != "hi" {
t.Fatalf("resynced user turn = %+v", user)
}
assistant := response.ChatMessages[1]
if assistant.Role != protocol.ChatRoleAssistant || assistant.ID != "msg_1" {
t.Fatalf("resynced assistant turn = %+v", assistant)
assertSeededChatMessages(t, response.ChatHistory.Messages)
if calls, _, _ := broker.messageQuery(); calls != 1 {
t.Fatalf("reconnect skipped authoritative history: %d calls", calls)
}
}
+11 -22
View File
@@ -48,6 +48,7 @@ type fakeChat struct {
messageCalls int
messageSessionID string
messageLimit int
messageCursor string
messages []protocol.ChatMessage
permissionCalls int
permissionSessionID string
@@ -182,13 +183,14 @@ func (f *fakeChat) Interrupt(context.Context, string) error {
return f.interruptErr
}
func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) {
func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.messageCalls++
f.messageSessionID = sessionID
f.messageLimit = limit
return f.messages, nil
f.messageCursor = cursor
return protocol.ChatHistoryPage{Messages: f.messages}, nil
}
// messageQuery reports the recorded chat_messages call count, session id and
@@ -502,6 +504,7 @@ func TestChatPromptWithoutBroker(t *testing.T) {
func TestChatMessagesResumesRetainedState(t *testing.T) {
broker := scriptedChat()
broker.messages = seededChatMessages()
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
@@ -518,27 +521,13 @@ func TestChatMessagesResumesRetainedState(t *testing.T) {
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatMessages || response.ID != "r2" || len(response.ChatMessages) != 2 {
if response.Type != protocol.ChatMessages || response.ID != "r2" || response.ChatHistory == nil {
t.Fatalf("resume response = %+v", response)
}
// The prompt is retained as a user turn and the assistant reply beside it, so
// a reconnecting client re-syncs a conversation that still says who sent
// what. The user turn carries the sending client's label (the generic
// fallback when the prompt did not name one).
user := response.ChatMessages[0]
if user.Role != protocol.ChatRoleUser || len(user.Parts) != 1 || user.Parts[0].Type != protocol.ChatPartText || user.Parts[0].Text != "hi" || user.Client != chatClientLabelFallback {
t.Fatalf("resumed user turn = %+v", user)
}
message := response.ChatMessages[1]
if message.ID != "msg_1" || message.Role != protocol.ChatRoleAssistant || len(message.Parts) != 2 {
t.Fatalf("resumed message = %+v", message)
}
if message.Parts[0].Type != protocol.ChatPartText || message.Parts[0].ID != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0) || message.Parts[0].Text != "hello world" {
t.Fatalf("resumed text part = %+v", message.Parts[0])
}
if message.Parts[1].Type != protocol.ChatPartTool || message.Parts[1].State == nil || message.Parts[1].State.Status != protocol.ChatToolCompleted {
t.Fatalf("resumed tool part = %+v", message.Parts[1])
if calls, _, _ := broker.messageQuery(); calls != 1 {
t.Fatalf("authoritative history was not read: %d calls", calls)
}
assertSeededChatMessages(t, response.ChatMessages)
}
// seededChatMessages is the stored user + assistant + tool session the
@@ -629,12 +618,12 @@ func TestChatMessagesThroughRealAdapter(t *testing.T) {
messageReads.Add(1)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[
{"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"},
{"id":"msg_assistant","time":{"created":20,"completed":30},"type":"assistant","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"content":[
{"type":"text","text":"here you go"},
{"type":"reasoning","text":"thinking"},
{"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}}
]}
]},
{"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"}
],"cursor":{"next":null}}`))
}))
defer fixture.Close()