249 lines
10 KiB
Go
249 lines
10 KiB
Go
// 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")
|
|
exited := flag.Bool("shell-exited", false, "serve the selected fixture shell as observed exited")
|
|
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.LargeChatFixtureHandlerWithShell(workspace, *exited), 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)
|
|
}
|
|
shellReadbacks, err := verifyShellReadback(ctx, endpoint, *exited)
|
|
if err != nil {
|
|
fatal(err)
|
|
}
|
|
<-ctx.Done()
|
|
shutdown, stop := context.WithTimeout(context.Background(), 5*time.Second)
|
|
pipelinesErr := pipelinesServer.Shutdown(shutdown)
|
|
upstreamErr := upstreamServer.Shutdown(shutdown)
|
|
stop()
|
|
if pipelinesErr != nil {
|
|
fatal(pipelinesErr)
|
|
}
|
|
if upstreamErr != nil {
|
|
fatal(upstreamErr)
|
|
}
|
|
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],
|
|
"shell_readbacks": shellReadbacks, "clean_shutdown": true, "shutdown_exit_status": 0,
|
|
})
|
|
}
|
|
|
|
func verifyShellReadback(ctx context.Context, endpoint string, exited bool) (map[string]any, error) {
|
|
evidence := map[string]any{"test_id": "fixture-shell-owned-and-foreign-refusal", "negative": []string{}}
|
|
ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(endpoint, "http")+"/connect", nil)
|
|
if err != nil {
|
|
return evidence, err
|
|
}
|
|
defer ws.Close(websocket.StatusNormalClosure, "fixture complete")
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatShells, ID: "fixture-shells", ChatQuery: &protocol.ChatQuery{SessionID: server.LargeChatFixtureSessionID}}); err != nil {
|
|
return evidence, err
|
|
}
|
|
var listed protocol.Message
|
|
if listed, err = readFixtureResponse(ctx, ws, protocol.ChatShells); err != nil {
|
|
return evidence, err
|
|
}
|
|
if !exited && (len(listed.ChatShells) != 1 || listed.ChatShells[0].ID != server.LargeChatFixtureShellID) {
|
|
return evidence, fmt.Errorf("fixture shell is not listed")
|
|
}
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatShellOutput, ID: "fixture-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: server.LargeChatFixtureSessionID, ShellID: server.LargeChatFixtureShellID, Limit: 64 << 10}}); err != nil {
|
|
return evidence, err
|
|
}
|
|
page, err := readFixtureResponse(ctx, ws, protocol.ChatShellOutput)
|
|
if err != nil {
|
|
return evidence, err
|
|
}
|
|
if page.ChatShellOutput == nil || page.ChatShellOutput.Output != server.LargeChatFixtureShellOutput || page.ChatShellOutput.Shell.Status != map[bool]string{true: "exited", false: "running"}[exited] {
|
|
return evidence, fmt.Errorf("unexpected shell readback: %+v", page.ChatShellOutput)
|
|
}
|
|
owned := map[string]any{"shell_id": page.ChatShellOutput.Shell.ID, "output": page.ChatShellOutput.Output, "cursor": page.ChatShellOutput.Cursor, "status": page.ChatShellOutput.Shell.Status}
|
|
if page.ChatShellOutput.Shell.Exit != nil {
|
|
owned["exit"] = *page.ChatShellOutput.Shell.Exit
|
|
}
|
|
evidence["owned"] = owned
|
|
negative := evidence["negative"].([]string)
|
|
negativeRequests := []protocol.Message{
|
|
{Type: protocol.ChatShells, ID: "foreign-list", ChatQuery: &protocol.ChatQuery{SessionID: server.LargeChatFixtureForeignSessionID}},
|
|
{Type: protocol.ChatShells, ID: "unknown-list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_unregistered_fixture"}},
|
|
{Type: protocol.ChatShellOutput, ID: "foreign-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: server.LargeChatFixtureSessionID, ShellID: server.LargeChatFixtureForeignShellID, Limit: 64 << 10}},
|
|
{Type: protocol.ChatShellOutput, ID: "unknown-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: "ses_unregistered_fixture", ShellID: server.LargeChatFixtureShellID, Limit: 64 << 10}},
|
|
}
|
|
for _, request := range negativeRequests {
|
|
if err := wsjson.Write(ctx, ws, request); err != nil {
|
|
return evidence, err
|
|
}
|
|
var response protocol.Message
|
|
if err := wsjson.Read(ctx, ws, &response); err != nil {
|
|
return evidence, err
|
|
}
|
|
if response.Type != protocol.Error || strings.Contains(response.Error, server.LargeChatFixtureForeignShellOutput) {
|
|
return evidence, fmt.Errorf("negative request %s leaked or succeeded: %+v", request.ID, response)
|
|
}
|
|
negative = append(negative, request.ID)
|
|
}
|
|
evidence["negative"] = negative
|
|
evidence["negative_count"] = len(negative)
|
|
return evidence, nil
|
|
}
|
|
|
|
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)
|
|
}
|