Preserve large chat visual fixture baseline
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
// Command pipelines-large-chat-fixture starts a foreground-only scratch backend
|
||||
// for visual verification of paged chat history. It never invokes a model.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := flag.String("addr", "127.0.0.1:0", "Pipelines scratch listen address")
|
||||
scratch := flag.String("scratch-dir", "", "required scratch directory for the database and workspace")
|
||||
flag.Parse()
|
||||
if *scratch == "" {
|
||||
fatal(fmt.Errorf("-scratch-dir is required"))
|
||||
}
|
||||
if err := os.MkdirAll(*scratch, 0700); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
workspace := filepath.Join(*scratch, "workspace")
|
||||
if err := os.MkdirAll(workspace, 0700); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
upstream, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
defer upstream.Close()
|
||||
upstreamURL := "http://" + upstream.Addr().String()
|
||||
upstreamServer := &http.Server{Handler: server.LargeChatFixtureHandler(workspace), ReadHeaderTimeout: 5 * time.Second}
|
||||
go func() { _ = upstreamServer.Serve(upstream) }()
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(*scratch, "pipelines.db"))
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if err := store.RecordOperatorSession(ctx, server.LargeChatFixtureSessionID, workspace, "Controlled large chat history", protocol.ChatSessionTagOperator); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
attached, err := opencode.OpenEndpointAttached(ctx, upstreamURL, server.LargeChatFixturePassword, "", filepath.Join(*scratch, "unused-pairing.json"))
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
listener, err := net.Listen("tcp", *addr)
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
endpoint := "http://" + listener.Addr().String()
|
||||
pipelinesServer := &http.Server{Handler: server.HandlerWithOptions(ctx, server.Options{Chat: opencode.NewChat(attached.Connection)}, store), ReadHeaderTimeout: 5 * time.Second}
|
||||
go func() { _ = pipelinesServer.Serve(listener) }()
|
||||
|
||||
readbacks, err := verifyBroker(ctx, endpoint)
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
info := buildinfo.Current()
|
||||
_ = json.NewEncoder(os.Stdout).Encode(map[string]any{
|
||||
"endpoint": endpoint, "upstream_endpoint": upstreamURL, "backend": attached.Connection.Backend,
|
||||
"revision": info.Commit, "session_id": server.LargeChatFixtureSessionID, "workspace": workspace,
|
||||
"newest_readback": readbacks[0], "older_readback": readbacks[1],
|
||||
})
|
||||
<-ctx.Done()
|
||||
shutdown, stop := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer stop()
|
||||
_ = pipelinesServer.Shutdown(shutdown)
|
||||
_ = upstreamServer.Shutdown(shutdown)
|
||||
}
|
||||
|
||||
func verifyBroker(ctx context.Context, endpoint string) ([2]map[string]any, error) {
|
||||
var readbacks [2]map[string]any
|
||||
cursor := ""
|
||||
seen := map[string]bool{}
|
||||
for page := 0; page < 3; page++ {
|
||||
ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(endpoint, "http")+"/connect", nil)
|
||||
if err != nil {
|
||||
return readbacks, err
|
||||
}
|
||||
ws.SetReadLimit(8 << 20)
|
||||
if page == 0 {
|
||||
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatSessions, ID: "fixture-sessions"}); err != nil {
|
||||
ws.CloseNow()
|
||||
return readbacks, err
|
||||
}
|
||||
sessions, err := readFixtureResponse(ctx, ws, protocol.ChatSessions)
|
||||
if err != nil {
|
||||
ws.CloseNow()
|
||||
return readbacks, err
|
||||
}
|
||||
if len(sessions.ChatSessions) != 1 || sessions.ChatSessions[0].ID != server.LargeChatFixtureSessionID {
|
||||
ws.CloseNow()
|
||||
return readbacks, fmt.Errorf("fixture session is not listed through the broker")
|
||||
}
|
||||
}
|
||||
request := protocol.Message{Type: protocol.ChatMessages, ID: fmt.Sprintf("fixture-%d", page), ChatQuery: &protocol.ChatQuery{SessionID: server.LargeChatFixtureSessionID, Limit: 2, Cursor: cursor}}
|
||||
if err := wsjson.Write(ctx, ws, request); err != nil {
|
||||
ws.CloseNow()
|
||||
return readbacks, err
|
||||
}
|
||||
response, err := readFixtureResponse(ctx, ws, protocol.ChatMessages)
|
||||
ws.CloseNow() // Explicit reconnects prove cursor state is not a hub tail.
|
||||
if err != nil {
|
||||
return readbacks, err
|
||||
}
|
||||
if response.Type != protocol.ChatMessages || response.ChatHistory == nil || len(response.ChatHistory.Messages) != 2 {
|
||||
return readbacks, fmt.Errorf("unexpected broker history response: %+v", response)
|
||||
}
|
||||
for _, message := range response.ChatHistory.Messages {
|
||||
if seen[message.ID] {
|
||||
return readbacks, fmt.Errorf("duplicate fixture message %s", message.ID)
|
||||
}
|
||||
seen[message.ID] = true
|
||||
}
|
||||
if page == 0 {
|
||||
newest := response.ChatHistory.Messages[1]
|
||||
if len(newest.Parts) == 0 || newest.Parts[len(newest.Parts)-1].Text != server.LargeChatFixtureNewestAnswer || !newest.Parts[0].State.Truncated {
|
||||
return readbacks, fmt.Errorf("newest fixture answer or projection missing")
|
||||
}
|
||||
readbacks[0] = map[string]any{"message_ids": []string{response.ChatHistory.Messages[0].ID, newest.ID}, "answer": newest.Parts[len(newest.Parts)-1].Text, "next_cursor": response.ChatHistory.NextCursor}
|
||||
}
|
||||
if page == 1 {
|
||||
older := response.ChatHistory.Messages[1]
|
||||
if len(older.Parts) == 0 || older.Parts[len(older.Parts)-1].Text != server.LargeChatFixtureOlderAnswer {
|
||||
return readbacks, fmt.Errorf("older fixture answer missing")
|
||||
}
|
||||
readbacks[1] = map[string]any{"message_ids": []string{response.ChatHistory.Messages[0].ID, older.ID}, "answer": older.Parts[len(older.Parts)-1].Text, "next_cursor": response.ChatHistory.NextCursor}
|
||||
}
|
||||
cursor = response.ChatHistory.NextCursor
|
||||
if response.ChatHistory.HasMore != (page < 2) {
|
||||
return readbacks, fmt.Errorf("invalid fixture continuation on page %d", page)
|
||||
}
|
||||
}
|
||||
if cursor != "" || len(seen) != 6 {
|
||||
return readbacks, fmt.Errorf("incomplete fixture traversal")
|
||||
}
|
||||
return readbacks, nil
|
||||
}
|
||||
|
||||
func readFixtureResponse(ctx context.Context, ws *websocket.Conn, want string) (protocol.Message, error) {
|
||||
for {
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, ws, &response); err != nil {
|
||||
return protocol.Message{}, err
|
||||
}
|
||||
if response.Type == protocol.Error {
|
||||
return protocol.Message{}, fmt.Errorf("broker response error: %s", response.Error)
|
||||
}
|
||||
if response.Type == want {
|
||||
return response, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fatal(err error) {
|
||||
fmt.Fprintln(os.Stderr, "pipelines-large-chat-fixture:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -1,13 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
@@ -17,51 +13,13 @@ import (
|
||||
// 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)
|
||||
}))
|
||||
fixture := httptest.NewServer(LargeChatFixtureHandler(t.TempDir()))
|
||||
defer fixture.Close()
|
||||
broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL})
|
||||
attached, err := opencode.OpenEndpointAttached(context.Background(), fixture.URL, LargeChatFixturePassword, "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
broker := opencode.NewChat(attached.Connection)
|
||||
ctx, _, srv, conns := dialChatServer(t, Options{Chat: broker}, 1)
|
||||
conn := conns[0]
|
||||
conn.SetReadLimit(8 << 20)
|
||||
@@ -69,7 +27,7 @@ func TestChatHistoryLargeFixtureWebsocketTraversalAndReconnect(t *testing.T) {
|
||||
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}})
|
||||
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "history", ChatQuery: &protocol.ChatQuery{SessionID: LargeChatFixtureSessionID, 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)
|
||||
@@ -88,7 +46,7 @@ func TestChatHistoryLargeFixtureWebsocketTraversalAndReconnect(t *testing.T) {
|
||||
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 {
|
||||
if newest.Parts[len(newest.Parts)-1].Text != LargeChatFixtureNewestAnswer || !newest.Parts[0].State.Truncated {
|
||||
t.Fatal("huge tool prevented newest answer")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LargeChatFixtureSessionID is the throwaway operator session exposed by the
|
||||
// foreground large-history fixture. It is not a model or production session.
|
||||
const LargeChatFixtureSessionID = "ses_large_chat_fixture"
|
||||
|
||||
// LargeChatFixturePassword authenticates the disposable fixture endpoint. It is
|
||||
// fixed fixture data, not an operator credential.
|
||||
const LargeChatFixturePassword = "large-chat-fixture"
|
||||
|
||||
const (
|
||||
LargeChatFixtureNewestAnswer = "Controlled newest answer: large history fixture"
|
||||
LargeChatFixtureOlderAnswer = "Controlled older answer: large history fixture"
|
||||
)
|
||||
|
||||
// LargeChatFixtureHandler implements the small authenticated V2 OpenCode surface
|
||||
// required to drive the real Pipelines chat broker during a visual history check.
|
||||
// It deliberately serves fixed, paged data: no model is invoked and no production
|
||||
// history is read or written.
|
||||
func LargeChatFixtureHandler(workspace string) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /api/info", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !largeChatFixtureAuthenticated(w, r) {
|
||||
return
|
||||
}
|
||||
writeLargeChatFixtureJSON(w, `{"version":"fixture","urls":[]}`)
|
||||
})
|
||||
mux.HandleFunc("GET /api/event", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !largeChatFixtureAuthenticated(w, r) {
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
if flush, ok := w.(http.Flusher); ok {
|
||||
flush.Flush()
|
||||
}
|
||||
})
|
||||
mux.HandleFunc("GET /api/session", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !largeChatFixtureAuthenticated(w, r) {
|
||||
return
|
||||
}
|
||||
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":[{"id":%q,"title":"Controlled large chat history","agent":"fixture","model":{"id":"fixture"},"location":{"directory":%q},"time":{"created":1,"updated":1}}]}`, LargeChatFixtureSessionID, workspace))
|
||||
})
|
||||
mux.HandleFunc("GET /api/session/"+LargeChatFixtureSessionID+"/message", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !largeChatFixtureAuthenticated(w, r) {
|
||||
return
|
||||
}
|
||||
page := 0
|
||||
switch r.URL.Query().Get("cursor") {
|
||||
case "":
|
||||
case "older+/=&":
|
||||
page = 1
|
||||
case "last":
|
||||
page = 2
|
||||
default:
|
||||
http.Error(w, "unknown opaque cursor", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
writeLargeChatFixturePage(w, page)
|
||||
})
|
||||
return mux
|
||||
}
|
||||
|
||||
func largeChatFixtureAuthenticated(w http.ResponseWriter, r *http.Request) bool {
|
||||
user, password, ok := r.BasicAuth()
|
||||
if !ok || user != "opencode" || password != LargeChatFixturePassword {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="large-chat-fixture"`)
|
||||
http.Error(w, "authentication required", http.StatusUnauthorized)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writeLargeChatFixtureJSON(w http.ResponseWriter, body string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(body))
|
||||
}
|
||||
|
||||
func writeLargeChatFixturePage(w http.ResponseWriter, page int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[`))
|
||||
for i := 0; i < 2; i++ {
|
||||
if i != 0 {
|
||||
_, _ = w.Write([]byte(","))
|
||||
}
|
||||
id := 6 - page*2 - i
|
||||
// Equal timestamps ensure the broker's verified descending V2 sequence,
|
||||
// not guessed timestamp ordering, determines transcript order.
|
||||
_, _ = fmt.Fprintf(w, `{"id":"msg_%d","type":"assistant","time":{"created":1},"content":[`, id)
|
||||
if page == 0 && i == 0 {
|
||||
_, _ = w.Write([]byte(`{"type":"tool","id":"huge","state":{"content":[{"type":"text","text":"`))
|
||||
chunk := strings.Repeat("x", 32<<10)
|
||||
for n := 0; n < 65; n++ {
|
||||
_, _ = w.Write([]byte(chunk))
|
||||
}
|
||||
_, _ = w.Write([]byte(`"}]}},`))
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
answer := fmt.Sprintf("fixture answer %d", id)
|
||||
if id == 6 {
|
||||
answer = LargeChatFixtureNewestAnswer
|
||||
} else if id == 4 {
|
||||
answer = LargeChatFixtureOlderAnswer
|
||||
}
|
||||
_, _ = fmt.Fprintf(w, `{"type":"text","text":%q}]}`, answer)
|
||||
}
|
||||
next := []string{`"older+/=&"`, `"last"`, "null"}[page]
|
||||
_, _ = fmt.Fprintf(w, `],"cursor":{"next":%s}}`, next)
|
||||
}
|
||||
Reference in New Issue
Block a user