diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index 0ffa3cb0..1d2e4408 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -12,6 +12,7 @@ import ( "syscall" "time" + "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/paths" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/server" @@ -36,7 +37,26 @@ func main() { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() artifactsRoot := filepath.Join(filepath.Dir(*db), "artifacts") - srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, server.Options{ArtifactsRoot: artifactsRoot}, store), ReadHeaderTimeout: 5 * time.Second} + options := server.Options{ArtifactsRoot: artifactsRoot} + // Chat brokers OpenCode over the owned endpoint. Streaming needs a + // URL-bearing, SSE-capable backend. Detect may select "v2" (the CLI + // advertises `api`), but Open never starts a serve process for v2 and so + // returns no event-stream URL: this build enables chat only when a + // connection carries a streamable endpoint (the owned v1 serve process). + // Otherwise chat stays disabled and every chat_* request is answered with a + // protocol error naming the missing configuration rather than hanging. + if backend, err := opencode.Detect(ctx, opencode.Executable()); err != nil { + log.Printf("OpenCode chat unavailable: %v", err) + } else if conn, err := opencode.Open(ctx, opencode.Executable(), "", backend); err != nil { + log.Printf("OpenCode chat unavailable: %v", err) + } else if conn.URL == "" { + conn.Close() + log.Printf("OpenCode chat unavailable: backend %q exposes no streamable event endpoint", backend) + } else { + options.Chat = opencode.NewChat(conn) + defer conn.Close() + } + srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, options, store), ReadHeaderTimeout: 5 * time.Second} defer server.WaitRuns(srv.Handler) go func() { <-ctx.Done() diff --git a/internal/author/skill/references/operations.md b/internal/author/skill/references/operations.md index bb716717..2359e8b5 100644 --- a/internal/author/skill/references/operations.md +++ b/internal/author/skill/references/operations.md @@ -175,6 +175,21 @@ traversal, absolute paths, symlink escapes and unknown runs/steps. - Do not invent artifact files or claim a stage produced one without the recorded evidence; the server observes the directory after the stage. +## Brokered OpenCode chat (unshipped) + +The protocol declares the brokered `chat_*` surface (`chat_sessions`, +`chat_create`, `chat_prompt`, `chat_messages`, `chat_interrupt`, `chat_reply`). The +server dispatches `chat_prompt`, `chat_messages`, `chat_interrupt` and `chat_reply`, +answers the declared `chat_sessions`/`chat_create` with an explicit protocol error +because they are not yet brokered, and pushes `chat_event` frames derived from +OpenCode's global `/api/event` firehose, with retained per-session/per-message state +so a reconnecting client re-syncs instead of replaying deltas. This is an internal, unshipped capability: no client consumes it, +and chat is enabled only when the OpenCode connection exposes a URL-bearing SSE +endpoint (the owned v1 serve process). On a V2 CLI (`opencode api`, no owned serve) +that endpoint is absent, so every dispatched `chat_*` request returns a protocol error +naming the missing configuration. Do not describe brokered chat as a finished or +user-facing feature. + ## Debug and retry Debug opens a new conversation with retained failure context, exact definition and diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go new file mode 100644 index 00000000..dd0cc386 --- /dev/null +++ b/internal/opencode/chat.go @@ -0,0 +1,355 @@ +package opencode + +import ( + "bufio" + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// 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. +type Chat struct { + conn *Connection +} + +// NewChat brokers chat for one owned OpenCode endpoint. The connection must be +// able to reach the /api HTTP surface: an owned serve connection (translated by +// Connection.Call) or a connection that already carries the v2 API URL. +func NewChat(conn *Connection) *Chat { return &Chat{conn: conn} } + +// ValidSessionID reports whether id has the OpenCode session-id shape the +// native adapter already accepts. It is the single validation shared by the +// chat adapter and callers that must reject a request before any OpenCode call. +func ValidSessionID(id string) bool { return sessionID.MatchString(id) } + +// Prompt forwards one chat prompt through the existing Call translation +// (authenticated, bounded, directory-scoped). The endpoint returns the inbox +// envelope immediately; the reply arrives on Stream. +func (c *Chat) Prompt(ctx context.Context, id, text string) error { + if !sessionID.MatchString(id) { + return fmt.Errorf("invalid OpenCode session id") + } + if strings.TrimSpace(text) == "" { + return fmt.Errorf("empty chat prompt") + } + body := map[string]any{"parts": []map[string]string{{"type": "text", "text": text}}} + if _, err := c.conn.Call(ctx, "post", "/api/session/"+id+"/prompt", body); err != nil { + return err + } + return nil +} + +// Interrupt aborts the session's active run, reusing the same translated surface. +func (c *Chat) Interrupt(ctx context.Context, id string) error { + if !sessionID.MatchString(id) { + return fmt.Errorf("invalid OpenCode session id") + } + if _, err := c.conn.Call(ctx, "post", "/api/session/"+id+"/interrupt", map[string]any{}); err != nil { + return err + } + return nil +} + +// Messages reads one session's stored turns. On the owned serve backend it +// 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) { + if !sessionID.MatchString(id) { + return nil, fmt.Errorf("invalid OpenCode session id") + } + var data []byte + var err error + 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) + } + data, err = c.conn.Call(ctx, "get", path, nil) + } + if err != nil { + return nil, err + } + return decodeChatMessages(id, data) +} + +// Reply posts a permission decision, mirroring the documented endpoint. +func (c *Chat) Reply(ctx context.Context, request protocol.ChatReplyRequest) error { + if !sessionID.MatchString(request.SessionID) { + return fmt.Errorf("invalid OpenCode session id") + } + if request.RequestID == "" { + return fmt.Errorf("missing permission request id") + } + decision := request.Decision + if decision != protocol.ChatDecisionOnce && decision != protocol.ChatDecisionAlways && decision != protocol.ChatDecisionReject { + return fmt.Errorf("invalid permission decision %q", decision) + } + body := map[string]any{"decision": decision} + if request.Message != "" { + body["message"] = request.Message + } + path := "/api/session/" + request.SessionID + "/permission/" + request.RequestID + "/reply" + if _, err := c.conn.Call(ctx, "post", path, body); err != nil { + return err + } + return nil +} + +// Stream subscribes to the OpenCode event firehose and invokes emit for every +// frame belonging to sessionID. It reads one bounded SSE frame at a time and +// never polls the per-session message log; the global firehose is the only +// source of token-level deltas on this build. ready, when non-nil, is called +// once the subscription is attached and before the first frame is read, so a +// caller can post its prompt only after the stream is listening (the firehose +// has no replay cursor). Canceling ctx cancels the subscription and returns +// without leaking a connection. +func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error { + if !sessionID.MatchString(id) { + return fmt.Errorf("invalid OpenCode session id") + } + body, err := c.conn.EventStream(ctx) + if err != nil { + return err + } + defer body.Close() + if ready != nil { + ready() + } + return scanChatEvents(ctx, body, id, emit) +} + +// scanChatEvents turns an SSE body into protocol frames, one bounded frame at a +// time. Heartbeat comments, blank lines and unrelated event types are ignored; +// malformed JSON fails the stream instead of producing a malformed frame. The +// last assistant message id is retained so a later tool/step event that omits it +// still attributes every frame to a stable message. +func scanChatEvents(ctx context.Context, body io.Reader, sessionID string, emit func(protocol.ChatEventFrame) error) error { + scanner := bufio.NewScanner(body) + scanner.Buffer(make([]byte, 0, 64<<10), MaxResponse) + lastMessageID := "" + for scanner.Scan() { + if err := ctx.Err(); err != nil { + return err + } + line := bytes.TrimSpace(scanner.Bytes()) + if !bytes.HasPrefix(line, []byte("data:")) { + continue + } + payload := bytes.TrimSpace(line[len("data:"):]) + if len(payload) == 0 { + continue + } + frame, ok, err := NormalizeChatEvent(payload, sessionID, &lastMessageID) + if err != nil { + return err + } + if !ok { + continue + } + if frame.MessageID != "" { + lastMessageID = frame.MessageID + } + if err := emit(frame); err != nil { + return err + } + } + if err := scanner.Err(); err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + return fmt.Errorf("OpenCode event stream read failed") + } + return ctx.Err() +} + +// chatEvent is the subset of the verified firehose envelope this adapter reads: +// {"type":"session.text.delta","data":{...}}. Fields absent from a given event +// stay zero so one struct covers every type. +type chatEvent struct { + SessionID string `json:"sessionID"` + MessageID string `json:"assistantMessageID"` + MessageIDAlt string `json:"messageID"` + Delta string `json:"delta"` + ID string `json:"id"` + Name string `json:"name"` + Input json.RawMessage `json:"input"` + Content []protocol.ChatToolContent `json:"content"` + Metadata map[string]any `json:"metadata"` + Error *protocol.ChatError `json:"error"` + Ordinal int `json:"ordinal"` + Index int `json:"index"` + Step int `json:"step"` + InputTokens int `json:"inputTokens"` + OutputTokens int `json:"outputTokens"` + Cost float64 `json:"cost"` + Tokens struct { + Input int `json:"input"` + Output int `json:"output"` + } `json:"tokens"` +} + +// NormalizeChatEvent maps one OpenCode firehose `data:` payload to a +// protocol.ChatEventFrame. ok is false for an event type this adapter does not +// surface or one belonging to a different session, so the caller can skip it +// without emitting a frame. Malformed JSON returns an error and never a frame. +func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string) (protocol.ChatEventFrame, bool, error) { + var outer struct { + Type string `json:"type"` + Data json.RawMessage `json:"data"` + } + if err := json.Unmarshal(payload, &outer); err != nil { + return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode event: %w", err) + } + if outer.Type == "" || len(outer.Data) == 0 { + return protocol.ChatEventFrame{}, false, nil + } + var data chatEvent + if err := json.Unmarshal(outer.Data, &data); err != nil { + return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode event data: %w", err) + } + if data.SessionID != sessionID { + return protocol.ChatEventFrame{}, false, nil + } + messageID := data.MessageID + if messageID == "" { + messageID = data.MessageIDAlt + } + if messageID == "" && lastMessageID != nil { + messageID = *lastMessageID + } + // Every pushed frame must carry a stable message id so the client can + // attribute it; without one this is not a chat frame. + if messageID == "" { + return protocol.ChatEventFrame{}, false, nil + } + frame := protocol.ChatEventFrame{SessionID: sessionID, MessageID: messageID} + switch { + case outer.Type == "session.text.delta": + frame.Kind = protocol.ChatEventText + frame.Delta = data.Delta + frame.Part = &protocol.ChatPart{Type: protocol.ChatPartText, ID: fmt.Sprintf("%s#%d", messageID, data.Ordinal)} + case outer.Type == "session.reasoning.delta": + frame.Kind = protocol.ChatEventReasoning + frame.Delta = data.Delta + frame.Part = &protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: fmt.Sprintf("%s#%d", messageID, data.Ordinal)} + case outer.Type == "session.step.started" || outer.Type == "session.step.streamed" || outer.Type == "session.step.ended": + frame.Kind = protocol.ChatEventStep + frame.Step = &protocol.ChatStep{Status: strings.TrimPrefix(outer.Type, "session.step."), Index: stepIndex(data)} + case strings.HasPrefix(outer.Type, "session.tool."): + status, ok := toolStatus(outer.Type) + if !ok { + return protocol.ChatEventFrame{}, false, nil + } + frame.Kind = protocol.ChatEventTool + state := &protocol.ChatToolState{Status: status, Input: data.Input, Content: data.Content, Metadata: data.Metadata, Error: data.Error} + frame.Part = &protocol.ChatPart{Type: protocol.ChatPartTool, ID: data.ID, Name: data.Name, State: state} + case outer.Type == "session.usage.updated": + frame.Kind = protocol.ChatEventUsage + input, output := data.InputTokens, data.OutputTokens + if data.Tokens.Input != 0 || data.Tokens.Output != 0 { + input, output = data.Tokens.Input, data.Tokens.Output + } + frame.Usage = &protocol.ChatUsage{InputTokens: input, OutputTokens: output, Cost: data.Cost} + default: + return protocol.ChatEventFrame{}, false, nil + } + return frame, true, nil +} + +// toolStatus maps the verified tool event types onto the ToolState lifecycle. +func toolStatus(eventType string) (string, bool) { + switch eventType { + case "session.tool.input.started", "session.tool.input.ended": + return protocol.ChatToolStreaming, true + case "session.tool.called", "session.tool.progress": + return protocol.ChatToolRunning, true + case "session.tool.success": + return protocol.ChatToolCompleted, true + case "session.tool.failed": + return protocol.ChatToolError, true + } + return "", false +} + +func stepIndex(data chatEvent) int { + if data.Step > 0 { + return data.Step + } + return data.Index +} + +// decodeChatMessages maps the native /message response onto the protocol chat +// history shape. Assistant content parts are text, reasoning and tool; a user +// turn is the prompt text. It never re-derives provider shapes. +type nativeChatMessage struct { + Info struct { + ID string `json:"id"` + Role string `json:"role"` + ModelID string `json:"modelID"` + Agent string `json:"agent"` + CreatedAt int64 `json:"created"` + } `json:"info"` + Parts []struct { + Type string `json:"type"` + ID string `json:"id"` + Text string `json:"text"` + Name string `json:"name"` + Executed bool `json:"executed"` + State *protocol.ChatToolState `json:"state"` + Time *protocol.ChatTime `json:"time"` + } `json:"parts"` +} + +// chatMessageRecords accepts both message envelopes the two surfaces return: +// the V2 HTTP API wraps them in {"data":[...]} while the native serve readback +// returns a bare array. An unrecognised body is an error, never empty history. +func chatMessageRecords(data []byte) ([]nativeChatMessage, error) { + var response struct { + Data []nativeChatMessage `json:"data"` + } + if err := json.Unmarshal(data, &response); err == nil && response.Data != nil { + return response.Data, nil + } + var records []nativeChatMessage + if err := json.Unmarshal(data, &records); err != nil { + return nil, fmt.Errorf("invalid OpenCode message response") + } + return records, nil +} + +func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, error) { + records, err := chatMessageRecords(data) + if err != nil { + return nil, err + } + messages := make([]protocol.ChatMessage, 0, len(records)) + for _, message := range records { + role := message.Info.Role + if role != protocol.ChatRoleUser && role != protocol.ChatRoleAssistant { + continue + } + out := protocol.ChatMessage{ID: message.Info.ID, SessionID: sessionID, Role: role, Model: message.Info.ModelID, Agent: message.Info.Agent, CreatedAt: message.Info.CreatedAt} + for _, part := range message.Parts { + switch part.Type { + case protocol.ChatPartText, protocol.ChatPartReasoning: + out.Parts = append(out.Parts, protocol.ChatPart{Type: part.Type, ID: part.ID, Text: part.Text}) + case protocol.ChatPartTool: + out.Parts = append(out.Parts, protocol.ChatPart{Type: protocol.ChatPartTool, ID: part.ID, Name: part.Name, Executed: part.Executed, State: part.State, Time: part.Time}) + } + } + messages = append(messages, out) + } + return messages, nil +} diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go new file mode 100644 index 00000000..ac78a477 --- /dev/null +++ b/internal/opencode/chat_test.go @@ -0,0 +1,292 @@ +package opencode + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func TestNormalizeChatEvent(t *testing.T) { + last := "msg_prev" + cases := []struct { + name string + payload string + last *string + wantOK bool + wantKind string + wantMsg string + wantDelta string + wantStatus string + wantStep string + wantUsage *protocol.ChatUsage + }{ + {name: "text delta", payload: `{"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"hel"}}`, wantOK: true, wantKind: protocol.ChatEventText, wantMsg: "msg_1", wantDelta: "hel"}, + {name: "reasoning delta", payload: `{"type":"session.reasoning.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":1,"delta":"hmm"}}`, wantOK: true, wantKind: protocol.ChatEventReasoning, wantMsg: "msg_1", wantDelta: "hmm"}, + {name: "step ended", payload: `{"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":2}}`, wantOK: true, wantKind: protocol.ChatEventStep, wantStep: protocol.ChatStepEnded}, + {name: "tool streaming", payload: `{"type":"session.tool.input.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":"{\"command\""}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolStreaming}, + {name: "tool running", payload: `{"type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolRunning}, + {name: "tool completed", payload: `{"type":"session.tool.success","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","content":[{"type":"text","text":"ok"}]}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolCompleted}, + {name: "tool failed", payload: `{"type":"session.tool.failed","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","error":{"type":"tool","message":"boom"}}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolError}, + {name: "usage", payload: `{"type":"session.usage.updated","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","tokens":{"input":12,"output":34},"cost":0.001}}`, wantOK: true, wantKind: protocol.ChatEventUsage, wantUsage: &protocol.ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001}}, + {name: "message id fallback", payload: `{"type":"session.step.started","data":{"sessionID":"ses_1","index":1}}`, last: &last, wantOK: true, wantKind: protocol.ChatEventStep, wantMsg: "msg_prev", wantStep: protocol.ChatStepStarted}, + {name: "foreign session skipped", payload: `{"type":"session.text.delta","data":{"sessionID":"ses_other","delta":"x"}}`, wantOK: false}, + {name: "unknown type skipped", payload: `{"type":"session.created","data":{"sessionID":"ses_1"}}`, wantOK: false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + lastID := tc.last + frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", lastID) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if ok != tc.wantOK { + t.Fatalf("ok = %v, want %v", ok, tc.wantOK) + } + if !ok { + return + } + if frame.SessionID != "ses_1" || frame.Kind != tc.wantKind { + t.Fatalf("frame = %+v", frame) + } + if tc.wantMsg != "" && frame.MessageID != tc.wantMsg { + t.Fatalf("messageID = %q, want %q", frame.MessageID, tc.wantMsg) + } + if tc.wantDelta != "" && frame.Delta != tc.wantDelta { + t.Fatalf("delta = %q, want %q", frame.Delta, tc.wantDelta) + } + if tc.wantStatus != "" && (frame.Part == nil || frame.Part.State == nil || frame.Part.State.Status != tc.wantStatus) { + t.Fatalf("tool state = %+v, want status %q", frame.Part, tc.wantStatus) + } + if tc.wantStep != "" && (frame.Step == nil || frame.Step.Status != tc.wantStep) { + t.Fatalf("step = %+v, want %q", frame.Step, tc.wantStep) + } + if tc.wantUsage != nil && (frame.Usage == nil || *frame.Usage != *tc.wantUsage) { + t.Fatalf("usage = %+v, want %+v", frame.Usage, tc.wantUsage) + } + }) + } +} + +func TestNormalizeChatEventRejectsMalformedJSON(t *testing.T) { + if _, _, err := NormalizeChatEvent([]byte(`{"type":`), "ses_1", nil); err == nil { + t.Fatal("malformed event must return an error, not a frame") + } +} + +func TestChatStreamFiltersFrames(t *testing.T) { + var prompts atomic.Int32 + frames := []string{ + `{"id":"evt_1","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"hello "}}`, + `{"id":"evt_2","type":"session.text.delta","data":{"sessionID":"ses_other","assistantMessageID":"msg_x","ordinal":0,"delta":"ignore"}}`, + `{"id":"evt_3","type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, + `{"id":"evt_4","type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":1}}`, + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/prompt": + prompts.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/event": + w.Header().Set("Content-Type", "text/event-stream") + for _, frame := range frames { + _, _ = w.Write([]byte("data: " + frame + "\n\n")) + } + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer srv.Close() + + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + var got []protocol.ChatEventFrame + err := chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error { + got = append(got, frame) + return nil + }) + if err != nil { + t.Fatalf("stream: %v", err) + } + if len(got) != 3 { + t.Fatalf("got %d frames, want 3 (foreign session filtered): %+v", len(got), got) + } + if got[0].Kind != protocol.ChatEventText || got[0].Delta != "hello " || got[0].MessageID != "msg_1" || got[0].Part == nil || got[0].Part.ID == "" { + t.Fatalf("text frame = %+v", got[0]) + } + if got[1].Kind != protocol.ChatEventTool || got[1].Part == nil || got[1].Part.State == nil || got[1].Part.State.Status != protocol.ChatToolRunning { + t.Fatalf("tool frame = %+v", got[1]) + } + if got[2].Kind != protocol.ChatEventStep || got[2].Step == nil || got[2].Step.Status != protocol.ChatStepEnded { + t.Fatalf("step frame = %+v", got[2]) + } + for _, frame := range got { + if frame.SessionID != "ses_1" || frame.MessageID == "" { + t.Fatalf("frame missing stable ids: %+v", frame) + } + } + + if err := chat.Prompt(ctx, "ses_1", "hello"); err != nil { + t.Fatalf("prompt: %v", err) + } + if prompts.Load() != 1 { + t.Fatalf("prompt posts = %d, want 1", prompts.Load()) + } +} + +func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) { + var calls atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx := context.Background() + if err := chat.Prompt(ctx, "not-a-session", "hi"); err == nil { + t.Fatal("invalid session id must be rejected") + } + if err := chat.Stream(ctx, "", nil, func(protocol.ChatEventFrame) error { return nil }); err == nil { + t.Fatal("empty session id must be rejected") + } + if err := chat.Interrupt(ctx, "bad id"); err == nil { + t.Fatal("invalid interrupt session id must be rejected") + } + if calls.Load() != 0 { + t.Fatalf("invalid requests reached the service: %d", calls.Load()) + } +} + +func TestDecodeChatMessages(t *testing.T) { + raw := []byte(`{"data":[{"info":{"id":"msg_1","role":"assistant","modelID":"m","agent":"build","created":10},"parts":[{"type":"text","id":"p1","text":"hi"},{"type":"reasoning","id":"p2","text":"hmm"},{"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}}]}]}`) + messages, err := decodeChatMessages("ses_1", raw) + if err != nil { + t.Fatal(err) + } + if len(messages) != 1 || messages[0].Role != protocol.ChatRoleAssistant || len(messages[0].Parts) != 3 { + t.Fatalf("messages = %+v", messages) + } + tool := messages[0].Parts[2] + if tool.Type != protocol.ChatPartTool || tool.Name != "shell" || tool.State == nil || tool.State.Status != protocol.ChatToolCompleted || tool.Time == nil { + t.Fatalf("tool part = %+v", tool) + } +} + +func TestChatStreamRejectsMalformedEvent(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"type\":\"session.text.delta\",\"data\":{\"sessionID\":\"ses_1\"\n\n")) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + emitted := 0 + err := chat.Stream(context.Background(), "ses_1", nil, func(protocol.ChatEventFrame) error { + emitted++ + return nil + }) + if err == nil { + t.Fatal("malformed event JSON must fail the stream") + } + if emitted != 0 { + t.Fatalf("malformed event produced %d frames", emitted) + } +} + +func TestChatStreamRequiresEndpoint(t *testing.T) { + chat := NewChat(&Connection{Backend: "v2"}) + err := chat.Stream(context.Background(), "ses_1", nil, func(protocol.ChatEventFrame) error { return nil }) + if err == nil || !strings.Contains(err.Error(), "event-stream endpoint") { + t.Fatalf("expected endpoint error, got %v", err) + } +} + +func TestNormalizeChatEventWithoutMessageID(t *testing.T) { + for _, payload := range []string{ + `{"type":"session.text.delta","data":{"sessionID":"ses_1","ordinal":0,"delta":"x"}}`, + `{"type":"session.tool.called","data":{"sessionID":"ses_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, + `{"type":"session.usage.updated","data":{"sessionID":"ses_1","tokens":{"input":1,"output":2}}}`, + } { + if _, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", nil); err != nil || ok { + t.Fatalf("event without a message id produced a frame: ok=%v err=%v", ok, err) + } + } +} + +// TestChatV1ConnectionDrivesThroughCall drives the adapter through a real +// (serve-shaped) Connection so the /api -> native translation and the owned +// endpoint are exercised, not a direct v2 URL shortcut. +func TestChatV1ConnectionDrivesThroughCall(t *testing.T) { + var sawPrompt, sawMessages bool + var promptBody string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/session/ses_1/message": + // Call must have translated /api/session/{id}/prompt to this path. + sawPrompt = true + body, _ := io.ReadAll(r.Body) + promptBody = string(body) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"ok":true}`)) + case r.Method == http.MethodGet && r.URL.Path == "/session/ses_1/message": + sawMessages = true + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[{"info":{"id":"msg_1","role":"assistant","modelID":"m","agent":"build","created":10},"parts":[{"type":"text","id":"p1","text":"hi"}]}]`)) + case r.Method == http.MethodGet && r.URL.Path == "/event": + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"type\":\"session.text.delta\",\"data\":{\"sessionID\":\"ses_1\",\"assistantMessageID\":\"msg_1\",\"ordinal\":0,\"delta\":\"hi\"}}\n\n")) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer srv.Close() + + chat := NewChat(&Connection{Backend: "v1", URL: srv.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := chat.Prompt(ctx, "ses_1", "hello"); err != nil { + t.Fatalf("prompt: %v", err) + } + if !sawPrompt { + t.Fatal("prompt was not translated to the native path") + } + // The translated body must carry the prompt text: the chat adapter sends the + // V2 `parts` shape and the v1 translation has to preserve it, or the native + // serve process receives an empty prompt. + var sent struct { + Parts []struct { + Type string `json:"type"` + Text string `json:"text"` + } `json:"parts"` + } + if err := json.Unmarshal([]byte(promptBody), &sent); err != nil { + t.Fatalf("prompt body: %v (%s)", err, promptBody) + } + if len(sent.Parts) != 1 || sent.Parts[0].Type != protocol.ChatPartText || sent.Parts[0].Text != "hello" { + t.Fatalf("prompt text not carried through the v1 translation: %s", promptBody) + } + var got []protocol.ChatEventFrame + if err := chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error { + got = append(got, frame) + return nil + }); err != nil { + t.Fatalf("stream: %v", err) + } + 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) + if err != nil { + t.Fatalf("messages: %v", err) + } + if !sawMessages || len(messages) != 1 || messages[0].ID != "msg_1" { + t.Fatalf("messages = %+v", messages) + } +} diff --git a/internal/opencode/connection.go b/internal/opencode/connection.go index ac01b802..fca80cb3 100644 --- a/internal/opencode/connection.go +++ b/internal/opencode/connection.go @@ -277,3 +277,40 @@ func (c *Connection) Native(ctx context.Context, method, path string, body any) } return data, nil } + +// EventStream opens the authenticated SSE firehose for the connection's owned +// endpoint using the same URL and credentials as Native, so chat never builds a +// parallel HTTP client. The path follows the backend surface: the native serve +// stream is /event, the V2 HTTP API is /api/event. The caller closes the body. +func (c *Connection) EventStream(ctx context.Context) (io.ReadCloser, error) { + if c == nil || c.URL == "" { + return nil, fmt.Errorf("OpenCode chat requires an event-stream endpoint") + } + path := "/api/event" + if c.Backend == "v1" { + path = "/event" + } + request, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimSuffix(c.URL, "/")+path, nil) + if err != nil { + return nil, err + } + request.Header.Set("Accept", "text/event-stream") + request.SetBasicAuth("pipelines", c.password) + response, err := http.DefaultClient.Do(request) + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, fmt.Errorf("OpenCode event stream request failed") + } + if response.StatusCode < 200 || response.StatusCode >= 300 { + evidence, _ := io.ReadAll(io.LimitReader(response.Body, 16<<10)) + response.Body.Close() + return nil, fmt.Errorf("OpenCode event stream failed (HTTP %d): %s", response.StatusCode, strings.TrimSpace(string(evidence))) + } + if !strings.HasPrefix(response.Header.Get("Content-Type"), "text/event-stream") { + response.Body.Close() + return nil, fmt.Errorf("OpenCode event stream returned a non-SSE response") + } + return response.Body, nil +} diff --git a/internal/opencode/native.go b/internal/opencode/native.go index 6f8842b9..5a4ed4fe 100644 --- a/internal/opencode/native.go +++ b/internal/opencode/native.go @@ -65,9 +65,14 @@ func messageText(data []byte) (string, error) { // Call adapts the pipeline's existing V2 command contract to the native stable API. // Sessions still use the same account/database; only our loopback serve process is owned. +// A connection whose backend already speaks the /api HTTP surface forwards the path +// unchanged: only the v1 serve surface needs the /api -> native translation below. func (c *Connection) Call(ctx context.Context, method, path string, body any) ([]byte, error) { if c.Backend != "v1" { - return nil, fmt.Errorf("native adapter requires the serve backend") + if c.URL == "" { + return nil, fmt.Errorf("native adapter requires the serve backend") + } + return c.Native(ctx, method, path, body) } if path == "/api/session" && method == "post" { data, _ := json.Marshal(body) @@ -152,6 +157,9 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ Prompt string `json:"prompt"` Text string `json:"text"` Format json.RawMessage `json:"format"` + Parts []struct { + Text string `json:"text"` + } `json:"parts"` } if err := json.Unmarshal(data, &request); err != nil { return nil, err @@ -160,6 +168,17 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ if text == "" { text = request.Text } + // The brokered chat adapter sends the V2 prompt shape (a parts array); + // accept it so the native translation carries the text unchanged instead + // of posting an empty prompt. + if text == "" { + for _, part := range request.Parts { + if part.Text != "" { + text = part.Text + break + } + } + } payload := map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}} if len(request.Format) > 0 { supported, err := c.supportsFormat(ctx, request.Format) diff --git a/internal/server/chat.go b/internal/server/chat.go new file mode 100644 index 00000000..08b579cc --- /dev/null +++ b/internal/server/chat.go @@ -0,0 +1,458 @@ +package server + +import ( + "context" + "log" + "strings" + "sync" + "time" + + "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// chatBroker is the server-side OpenCode chat seam. *opencode.Chat implements +// it; tests substitute a scripted broker. Keeping it an interface means the +// /connect handler never builds a second OpenCode transport. +type chatBroker interface { + Prompt(ctx context.Context, sessionID, text string) error + // Stream subscribes to the session's OpenCode event stream. ready is called + // once the subscription is attached, before any frame is read, so the caller + // 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) + Reply(ctx context.Context, request protocol.ChatReplyRequest) error +} + +// chatRetainedLimit bounds the state one session may retain so a stalled reader +// or a runaway turn cannot grow memory without limit. It matches the OpenCode +// response bound (1 MiB). +const chatRetainedLimit = opencode.MaxResponse + +// chatMaxTurns bounds how many sessions retain turn state. A long-lived server +// cannot accumulate state for every session ever prompted, so the +// least-recently-updated turn is evicted once the cap is exceeded. +const chatMaxTurns = 128 + +// chatHub retains the accumulated turn state per session, independently of any +// single connection, so a reconnecting client can re-sync what it missed. State +// is keyed by session and then by message id (a session holds many turns), and +// is capped at chatRetainedLimit. +type chatHub struct { + mu sync.Mutex + turns map[string]*chatTurn + now func() time.Time +} + +// chatPart is the retained form of one part. Text and reasoning deltas append to +// text (amortized O(1)); a tool frame replaces part outright. +type chatPart struct { + part protocol.ChatPart + text []byte +} + +// chatMessage is one retained assistant turn. Its parts keep the exact ids the +// supporting chat_event frames carried, so a resume preserves attribution. +type chatMessage struct { + id string + order []string + parts map[string]*chatPart +} + +type chatTurn struct { + sessionID string + order []string + messages map[string]*chatMessage + step *protocol.ChatStep + usage *protocol.ChatUsage + size int + overflow bool + updated time.Time +} + +func newChatHub() *chatHub { + return &chatHub{turns: map[string]*chatTurn{}, now: time.Now} +} + +func (h *chatHub) clock() time.Time { + if h.now != nil { + return h.now() + } + return time.Now() +} + +// apply folds one pushed frame into the retained state. Text and reasoning +// deltas append to their message's part; a tool frame upserts the full part +// (accounting for its whole size) so the latest lifecycle status, input, +// content, metadata, error and time are retained under the frame's message id. +// Step and usage frames update the session's latest progress. +func (h *chatHub) apply(frame protocol.ChatEventFrame) { + if frame.SessionID == "" { + return + } + h.mu.Lock() + defer h.mu.Unlock() + turn := h.turns[frame.SessionID] + if turn == nil { + h.evictLocked() + turn = &chatTurn{sessionID: frame.SessionID, messages: map[string]*chatMessage{}} + h.turns[frame.SessionID] = turn + } + turn.updated = h.clock() + switch frame.Kind { + case protocol.ChatEventStep: + turn.step = frame.Step + return + case protocol.ChatEventUsage: + turn.usage = frame.Usage + return + } + if frame.MessageID == "" { + return + } + message := turn.messages[frame.MessageID] + if message == nil { + message = &chatMessage{id: frame.MessageID, parts: map[string]*chatPart{}} + turn.order = append(turn.order, frame.MessageID) + turn.messages[frame.MessageID] = message + } + switch frame.Kind { + case protocol.ChatEventText, protocol.ChatEventReasoning: + id := "" + if frame.Part != nil { + id = frame.Part.ID + } + if id == "" || frame.Delta == "" { + return + } + if !turn.retain(len(frame.Delta)) { + return + } + part, ok := message.parts[id] + if !ok { + part = &chatPart{part: protocol.ChatPart{Type: frame.Kind, ID: id}} + message.order = append(message.order, id) + message.parts[id] = part + } + part.text = append(part.text, frame.Delta...) + case protocol.ChatEventTool: + if frame.Part == nil || frame.Part.ID == "" || turn.overflow { + return + } + // The tool part is retained whole, so size the complete part (input, + // content, metadata, error and time) and charge the difference against + // the cap on every upsert, not just the first-seen name. + incoming := chatPartBytes(frame.Part) + part, ok := message.parts[frame.Part.ID] + if !ok { + if !turn.retain(incoming) { + return + } + part = &chatPart{} + message.order = append(message.order, frame.Part.ID) + message.parts[frame.Part.ID] = part + } else if previous := chatPartBytes(&part.part); incoming > previous { + if !turn.retain(incoming - previous) { + return + } + } else { + turn.shrink(previous - incoming) + } + part.part = *frame.Part + } +} + +// chatPartBytes estimates the retained size of a part so the per-session cap +// accounts for its whole state instead of only the first-seen name. It walks the +// fields rather than re-marshalling, so the cost matches the bytes it stores. +func chatPartBytes(part *protocol.ChatPart) int { + if part == nil { + return 0 + } + n := len(part.Type) + len(part.ID) + len(part.Name) + if part.State == nil { + return n + } + n += len(part.State.Status) + len(part.State.Input) + for _, content := range part.State.Content { + n += len(content.Type) + len(content.Text) + len(content.URI) + len(content.Mime) + len(content.Name) + } + if part.State.Error != nil { + n += len(part.State.Error.Type) + len(part.State.Error.Message) + } + for key, value := range part.State.Metadata { + n += len(key) + if text, ok := value.(string); ok { + n += len(text) + } else { + n += 8 + } + } + return n +} + +// evictLocked drops the least-recently-updated turn when the session cap is +// reached, keeping retained memory bounded across session churn. +func (h *chatHub) evictLocked() { + if len(h.turns) < chatMaxTurns { + return + } + oldestID := "" + var oldest time.Time + for id, turn := range h.turns { + if oldestID == "" || turn.updated.Before(oldest) { + oldestID, oldest = id, turn.updated + } + } + if oldestID != "" { + delete(h.turns, oldestID) + } +} + +// retain records n more bytes and reports whether the turn is still within the +// bound. The first overflow records one diagnostic and then drops further state +// instead of growing without limit. +func (t *chatTurn) retain(n int) bool { + if t.overflow { + return false + } + if t.size+n > chatRetainedLimit { + t.overflow = true + log.Printf("chat session %s retained state exceeds %d bytes; dropping further state", t.sessionID, chatRetainedLimit) + return false + } + t.size += n + return true +} + +// shrink releases n bytes when a tool upsert replaces a larger retained part. +func (t *chatTurn) shrink(n int) { + t.size -= n + if t.size < 0 { + t.size = 0 + } +} + +// snapshot returns the accumulated session as one assistant message per +// retained message id, with the same stable part ids, or nil when nothing is +// retained. It is the resume source: a reconnecting client re-syncs the whole +// session once instead of replaying deltas, and each turn keeps its own id. +func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage { + h.mu.Lock() + defer h.mu.Unlock() + turn := h.turns[sessionID] + if turn == nil { + return nil + } + turn.updated = h.clock() + var messages []protocol.ChatMessage + for _, id := range turn.order { + message := turn.messages[id] + if message == nil || len(message.order) == 0 { + continue + } + out := protocol.ChatMessage{ID: id, SessionID: sessionID, Role: protocol.ChatRoleAssistant} + for _, partID := range message.order { + if part, ok := message.parts[partID]; ok { + piece := part.part + piece.Text = string(part.text) + out.Parts = append(out.Parts, piece) + } + } + messages = append(messages, out) + } + return messages +} + +// handleChat dispatches one chat_* request. It returns true when the request was +// a chat type and has been fully answered (the caller must not fall through to +// the generic response). Frames are pushed after the ack, each through the +// connection-scoped write function so the read loop and the stream goroutine +// never interleave partial websocket writes. +func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) bool { + switch request.Type { + case protocol.ChatPrompt: + handleChatPrompt(ctx, request, broker, hub, write) + return true + case protocol.ChatMessages: + handleChatMessages(ctx, request, broker, hub, write) + return true + case protocol.ChatInterrupt: + handleChatSimple(ctx, request, broker, hub, write) + return true + case protocol.ChatReply: + handleChatSimple(ctx, request, broker, hub, write) + return true + case protocol.ChatSessions, protocol.ChatCreate: + // Declared in the protocol for the client but not yet brokered. Answer + // explicitly so the client never mistakes the generic Pong for a chat + // reply, and the boundary stays visible instead of silently dropping. + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat_sessions and chat_create are not yet brokered"}) + return true + } + return false +} + +func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { + if broker == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"}) + return + } + prompt := request.ChatPrompt + if prompt == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat prompt payload is required"}) + return + } + if !opencode.ValidSessionID(prompt.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + if strings.TrimSpace(prompt.Text) == "" { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a nonempty chat prompt is required"}) + return + } + // Reject an oversized prompt before any OpenCode call; the bound matches the + // OpenCode request limit. + if len(prompt.Text) > opencode.MaxRequest { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"}) + return + } + + // Subscribe to the OpenCode firehose before posting the prompt: the stream + // has no replay cursor (research 2.4), so a delta emitted between the POST + // and the subscription would otherwise be lost. Frames that arrive before the + // ack is written are buffered and flushed in order after it. + sessionID := prompt.SessionID + streamCtx, streamCancel := context.WithCancel(ctx) + attached := make(chan struct{}) + var attachOnce sync.Once + streamResult := make(chan error, 1) + var bufferMu sync.Mutex + buffering := true + var buffered []protocol.ChatEventFrame + go func() { + streamResult <- broker.Stream(streamCtx, sessionID, func() { + attachOnce.Do(func() { close(attached) }) + }, func(frame protocol.ChatEventFrame) error { + bufferMu.Lock() + if buffering { + buffered = append(buffered, frame) + bufferMu.Unlock() + return nil + } + bufferMu.Unlock() + hub.apply(frame) + return write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}) + }) + }() + + select { + case <-attached: + case err := <-streamResult: + streamCancel() + if err != nil && ctx.Err() == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat stream: " + err.Error()}) + } + return + case <-ctx.Done(): + streamCancel() + return + } + + if err := broker.Prompt(ctx, sessionID, prompt.Text); err != nil { + streamCancel() + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + // Acknowledge the accepted prompt with the echoed request id, then stream the + // reply. The prompt is sent exactly once and never re-sent on resume. + if err := write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}); err != nil { + streamCancel() + return + } + // Flush frames buffered while the prompt was accepted, then resume live + // delivery. Holding bufferMu preserves order: the stream goroutine either + // buffered before this flush or waits and writes after it. + bufferMu.Lock() + buffering = false + pending := buffered + buffered = nil + for _, frame := range pending { + hub.apply(frame) + if err := write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}); err != nil { + bufferMu.Unlock() + streamCancel() + return + } + } + bufferMu.Unlock() + go drainChatStream(ctx, streamResult, streamCancel, sessionID, write) +} + +// drainChatStream reports the terminal error of a chat subscription and releases +// its context once the stream has ended. +func drainChatStream(ctx context.Context, result <-chan error, cancel context.CancelFunc, sessionID string, write func(protocol.Message) error) { + defer cancel() + if err := <-result; err != nil && ctx.Err() == nil { + log.Printf("chat stream session=%s: %v", sessionID, err) + _ = write(protocol.Message{Type: protocol.Error, Error: "chat stream: " + err.Error()}) + } +} + +func handleChatMessages(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { + query := request.ChatQuery + if query == nil || !opencode.ValidSessionID(query.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + 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 + } + if broker == nil { + _ = write(response) + return + } + messages, err := broker.Messages(ctx, query.SessionID, query.Limit) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + response.ChatMessages = messages + _ = write(response) +} + +func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { + if broker == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"}) + return + } + if request.Type == protocol.ChatInterrupt { + query := request.ChatQuery + if query == nil || !opencode.ValidSessionID(query.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + if err := broker.Interrupt(ctx, query.SessionID); err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + _ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID}) + return + } + reply := request.ChatReply + if reply == nil || !opencode.ValidSessionID(reply.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + if err := broker.Reply(ctx, *reply); err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + _ = write(protocol.Message{Type: protocol.ChatReply, ID: request.ID}) +} diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go new file mode 100644 index 00000000..f300575b --- /dev/null +++ b/internal/server/chat_test.go @@ -0,0 +1,563 @@ +package server + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/coder/websocket" + "github.com/coder/websocket/wsjson" + + "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// fakeChat is a scripted chatBroker. It counts calls and either emits a fixed +// frame sequence or blocks until the caller's context is canceled. +type fakeChat struct { + mu sync.Mutex + promptCalls int + interrupts int + replies int + messages []protocol.ChatMessage + script []protocol.ChatEventFrame + started chan struct{} + done chan struct{} + startOnce sync.Once + block bool + subscribed bool + promptBeforeSubscribe bool +} + +func (f *fakeChat) Prompt(context.Context, string, string) error { + f.mu.Lock() + defer f.mu.Unlock() + f.promptCalls++ + if !f.subscribed { + f.promptBeforeSubscribe = true + } + return nil +} + +func (f *fakeChat) Stream(ctx context.Context, _ string, ready func(), emit func(protocol.ChatEventFrame) error) error { + f.mu.Lock() + f.subscribed = true + f.mu.Unlock() + if ready != nil { + ready() + } + if f.started != nil { + f.startOnce.Do(func() { close(f.started) }) + } + if f.block { + <-ctx.Done() + if f.done != nil { + close(f.done) + } + return ctx.Err() + } + for _, frame := range f.script { + if err := ctx.Err(); err != nil { + return err + } + if err := emit(frame); err != nil { + return err + } + } + return nil +} + +func (f *fakeChat) Interrupt(context.Context, string) error { + f.mu.Lock() + defer f.mu.Unlock() + f.interrupts++ + return nil +} + +func (f *fakeChat) Messages(context.Context, string, int) ([]protocol.ChatMessage, error) { + return f.messages, nil +} + +func (f *fakeChat) Reply(context.Context, protocol.ChatReplyRequest) error { + f.mu.Lock() + defer f.mu.Unlock() + f.replies++ + return nil +} + +func (f *fakeChat) counts() (int, int, int) { + f.mu.Lock() + defer f.mu.Unlock() + return f.promptCalls, f.interrupts, f.replies +} + +func chatFrame(kind, delta string, part *protocol.ChatPart, step *protocol.ChatStep) protocol.ChatEventFrame { + return protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: kind, Delta: delta, Part: part, Step: step} +} + +func scriptedChat() *fakeChat { + return &fakeChat{script: []protocol.ChatEventFrame{ + chatFrame(protocol.ChatEventText, "hello ", &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}, nil), + chatFrame(protocol.ChatEventText, "world", &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}, nil), + chatFrame(protocol.ChatEventTool, "", &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Input: []byte(`{"command":"ls"}`)}}, nil), + chatFrame(protocol.ChatEventStep, "", nil, &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: 1}), + }} +} + +func dialChat(t *testing.T, broker chatBroker) (context.Context, context.CancelFunc, *websocket.Conn) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: broker})) + t.Cleanup(func() { srv.Close(); cancel() }) + conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { conn.CloseNow() }) + return ctx, cancel, conn +} + +func readChatTurn(t *testing.T, ctx context.Context, conn *websocket.Conn) []protocol.ChatEventFrame { + t.Helper() + var frames []protocol.ChatEventFrame + for { + var message protocol.Message + if err := wsjson.Read(ctx, conn, &message); err != nil { + t.Fatal(err) + } + if message.Type == protocol.Error { + t.Fatalf("unexpected error frame: %s", message.Error) + } + if message.ChatEvent == nil { + continue + } + frames = append(frames, *message.ChatEvent) + if message.ChatEvent.Kind == protocol.ChatEventStep && message.ChatEvent.Step != nil && message.ChatEvent.Step.Status == protocol.ChatStepEnded { + return frames + } + } +} + +func TestChatPromptStreamsEvents(t *testing.T) { + broker := scriptedChat() + 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) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + if ack.Type != protocol.ChatPrompt || ack.ID != "r1" { + t.Fatalf("ack = %+v", ack) + } + frames := readChatTurn(t, ctx, conn) + if len(frames) != 4 { + t.Fatalf("got %d frames, want 4", len(frames)) + } + sawText, sawStep := false, false + for _, frame := range frames { + if frame.SessionID != "ses_1" || frame.MessageID != "msg_1" { + t.Fatalf("frame missing stable ids: %+v", frame) + } + switch frame.Kind { + case protocol.ChatEventText: + sawText = true + case protocol.ChatEventStep: + sawStep = true + if frame.Step == nil || frame.Step.Status != protocol.ChatStepEnded { + t.Fatalf("step frame = %+v", frame) + } + } + } + if !sawText || !sawStep { + t.Fatalf("missing text or step frame: %+v", frames) + } + if prompts, _, _ := broker.counts(); prompts != 1 { + t.Fatalf("prompt forwarded %d times, want 1", prompts) + } +} + +func TestChatPromptRejectsInvalidSession(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + for _, id := range []string{"", "not-a-session", "msg_1"} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "bad", ChatPrompt: &protocol.ChatPromptRequest{SessionID: id, Text: "hi"}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || response.ID != "bad" { + t.Fatalf("session %q not rejected: %+v", id, response) + } + } + if prompts, _, _ := broker.counts(); prompts != 0 { + t.Fatalf("invalid sessions reached OpenCode: %d", prompts) + } +} + +func TestChatPromptRejectsOversizedText(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + oversized := strings.Repeat("x", opencode.MaxRequest+1) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "big", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: oversized}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || !strings.Contains(response.Error, "64 KiB") { + t.Fatalf("oversized prompt not rejected: %+v", response) + } + if prompts, _, _ := broker.counts(); prompts != 0 { + t.Fatalf("oversized prompt reached OpenCode: %d", prompts) + } +} + +func TestChatPromptWithoutBroker(t *testing.T) { + ctx, _, conn := dialChat(t, nil) + 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) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || !strings.Contains(response.Error, "not configured") { + t.Fatalf("unconfigured chat not rejected: %+v", response) + } +} + +func TestChatMessagesResumesRetainedState(t *testing.T) { + broker := scriptedChat() + 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) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + readChatTurn(t, ctx, conn) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "r2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.ChatMessages || response.ID != "r2" || len(response.ChatMessages) != 1 { + t.Fatalf("resume response = %+v", response) + } + message := response.ChatMessages[0] + 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 != "msg_1#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]) + } +} + +func TestChatDisconnectCancelsStream(t *testing.T) { + broker := scriptedChat() + broker.block = true + broker.started = make(chan struct{}) + broker.done = make(chan struct{}) + 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) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + select { + case <-broker.started: + case <-time.After(2 * time.Second): + t.Fatal("stream never started") + } + conn.CloseNow() + // The server's read loop returns on disconnect, canceling the connection + // context the subscription observes; no goroutine may keep running. + select { + case <-broker.done: + case <-time.After(3 * time.Second): + t.Fatal("chat stream was not canceled on disconnect") + } + if prompts, _, _ := broker.counts(); prompts != 1 { + t.Fatalf("prompt forwarded %d times, want 1", prompts) + } +} + +func TestChatInterruptAndReply(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatInterrupt, ID: "i1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.ChatInterrupt || response.ID != "i1" { + t.Fatalf("interrupt response = %+v", response) + } + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "p1", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}}); err != nil { + t.Fatal(err) + } + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.ChatReply || response.ID != "p1" { + t.Fatalf("reply response = %+v", response) + } + if _, interrupts, replies := broker.counts(); interrupts != 1 || replies != 1 { + t.Fatalf("interrupts=%d replies=%d, want 1/1", interrupts, replies) + } +} + +func TestChatHubAccumulatesAndEvicts(t *testing.T) { + tick := 0 + hub := newChatHub() + hub.now = func() time.Time { tick++; return time.Unix(int64(tick), 0) } + for _, delta := range []string{"a", "b", "c"} { + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventText, Delta: delta, Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}}) + } + messages := hub.snapshot("ses_1") + if len(messages) != 1 || len(messages[0].Parts) != 1 || messages[0].Parts[0].Text != "abc" { + t.Fatalf("snapshot = %+v", messages) + } + for i := 0; i < chatMaxTurns+5; i++ { + id := fmt.Sprintf("ses_%d", i) + hub.apply(protocol.ChatEventFrame{SessionID: id, MessageID: "m", Kind: protocol.ChatEventText, Delta: "x", Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: id + "#0"}}) + } + if len(hub.turns) != chatMaxTurns { + t.Fatalf("retained %d turns, want %d", len(hub.turns), chatMaxTurns) + } +} + +// TestChatHubKeepsMessagesSeparate pins per-message retention: a second prompt +// in one session must not fold its parts onto the first turn's message id. +func TestChatHubKeepsMessagesSeparate(t *testing.T) { + hub := newChatHub() + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventText, Delta: "one", Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}}) + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_2", Kind: protocol.ChatEventText, Delta: "two", Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_2#0"}}) + messages := hub.snapshot("ses_1") + if len(messages) != 2 { + t.Fatalf("messages = %+v", messages) + } + if messages[0].ID != "msg_1" || messages[0].Parts[0].ID != "msg_1#0" || messages[0].Parts[0].Text != "one" { + t.Fatalf("first message = %+v", messages[0]) + } + if messages[1].ID != "msg_2" || messages[1].Parts[0].ID != "msg_2#0" || messages[1].Parts[0].Text != "two" { + t.Fatalf("second message = %+v", messages[1]) + } +} + +// TestChatHubChargesToolStateBytes pins the retained-state cap: a tool part's +// input/content bytes count against it, and an upsert that would exceed the cap +// is dropped with the overflow diagnostic rather than growing memory. +func TestChatHubChargesToolStateBytes(t *testing.T) { + hub := newChatHub() + big := strings.Repeat("x", chatRetainedLimit/2) + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventTool, Part: &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolStreaming, Input: json.RawMessage(`"` + big + `"`)}}}) + hub.mu.Lock() + size := hub.turns["ses_1"].size + hub.mu.Unlock() + if size < len(big) { + t.Fatalf("tool input not charged: size=%d want >= %d", size, len(big)) + } + huge := strings.Repeat("y", chatRetainedLimit) + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventTool, Part: &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Input: json.RawMessage(`"` + huge + `"`)}}}) + messages := hub.snapshot("ses_1") + if len(messages) != 1 || len(messages[0].Parts) != 1 || messages[0].Parts[0].State == nil || messages[0].Parts[0].State.Status != protocol.ChatToolStreaming { + t.Fatalf("overflowing tool state was retained: %+v", messages) + } +} + +// TestChatPromptSubscribesBeforePrompt pins the subscribe-before-prompt order: +// the firehose has no replay cursor, so the subscription must be attached before +// the prompt is posted or deltas emitted in between are lost. +func TestChatPromptSubscribesBeforePrompt(t *testing.T) { + broker := scriptedChat() + 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) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + readChatTurn(t, ctx, conn) + broker.mu.Lock() + before := broker.promptBeforeSubscribe + broker.mu.Unlock() + if before { + t.Fatal("prompt was posted before the event subscription was attached") + } +} + +// realAdapterChat posts a chat_prompt through the real opencode.Chat broker and +// HandlerWithOptions against a v2 HTTP fixture that replays frames, so the +// acceptance path forwards to an actual OpenCode-shaped stream (not a scripted +// fake) and returns the chat_event frames the server pushed. It pins exactly one +// prompt POST and one event subscription per chat_prompt, then asserts the ack +// echoes the request id; callers assert the frames themselves. +func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame { + t.Helper() + var prompts, streams atomic.Int32 + fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/prompt": + prompts.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/event": + streams.Add(1) + w.Header().Set("Content-Type", "text/event-stream") + for _, frame := range frames { + _, _ = w.Write([]byte("data: " + frame + "\n\n")) + } + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer fixture.Close() + + broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL}) + 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: "ping"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + if ack.Type != protocol.ChatPrompt || ack.ID != "r1" { + t.Fatalf("ack = %+v", ack) + } + framesIn := readChatTurn(t, ctx, conn) + if prompts.Load() != 1 || streams.Load() != 1 { + t.Fatalf("prompts=%d streams=%d, want 1/1", prompts.Load(), streams.Load()) + } + return framesIn +} + +// assertStableFrameIDs fails unless every frame carries the fixture's session +// and message ids: a client cannot attribute a frame without them. +func assertStableFrameIDs(t *testing.T, frames []protocol.ChatEventFrame) { + t.Helper() + for _, frame := range frames { + if frame.SessionID != "ses_1" || frame.MessageID != "msg_1" { + t.Fatalf("frame missing stable ids: %+v", frame) + } + } +} + +// TestChatPromptThroughRealAdapter drives a text, tool and step turn end to end. +func TestChatPromptThroughRealAdapter(t *testing.T) { + framesIn := realAdapterChat(t, []string{ + `{"id":"evt_1","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"pong"}}`, + `{"id":"evt_2","type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, + `{"id":"evt_3","type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":1}}`, + }) + assertStableFrameIDs(t, framesIn) + sawText, sawTool := false, false + for _, frame := range framesIn { + if frame.Kind == protocol.ChatEventText && frame.Delta == "pong" { + sawText = true + } + if frame.Kind == protocol.ChatEventTool && frame.Part != nil && frame.Part.Name == "shell" { + sawTool = true + } + } + if !sawText || !sawTool { + t.Fatalf("missing text or tool frame: %+v", framesIn) + } +} + +// TestChatPromptNoToolThroughRealAdapter is the task's evidence harness: it posts +// a trivial no-tool prompt to the real opencode.Chat adapter over a v2 SSE +// fixture that emits only a text delta and a step, and asserts both arrive as +// chat_event frames with stable session/message ids. It reuses dialChat, +// readChatTurn and realAdapterChat so it pins the same ack/stream contract as the +// other chat tests, and it pins exactly one prompt POST and one event subscription. +func TestChatPromptNoToolThroughRealAdapter(t *testing.T) { + framesIn := realAdapterChat(t, []string{ + `{"id":"evt_1","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"pong"}}`, + `{"id":"evt_2","type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":1}}`, + }) + assertStableFrameIDs(t, framesIn) + sawText, sawStep, sawTool := false, false, false + for _, frame := range framesIn { + switch frame.Kind { + case protocol.ChatEventText: + sawText = frame.Delta == "pong" + case protocol.ChatEventStep: + sawStep = frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded + case protocol.ChatEventTool: + sawTool = true + } + } + if !sawText || !sawStep { + t.Fatalf("missing text or step frame: %+v", framesIn) + } + if sawTool { + t.Fatalf("a trivial no-tool prompt produced a tool frame: %+v", framesIn) + } +} + +// TestChatPromptRejectsEmptyText pins the whitespace guard: an empty or +// whitespace-only prompt is rejected before any OpenCode call. +func TestChatPromptRejectsEmptyText(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + for _, text := range []string{"", " ", "\t\n"} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "empty", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: text}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || response.ID != "empty" { + t.Fatalf("empty text %q not rejected: %+v", text, response) + } + } + if prompts, _, _ := broker.counts(); prompts != 0 { + t.Fatalf("empty prompts reached OpenCode: %d", prompts) + } +} + +// TestChatSessionsAndCreateAnswerExplicitly pins the declared-but-unbrokered +// boundary: chat_sessions and chat_create are answered with a protocol error +// rather than falling through to the generic Pong. +func TestChatSessionsAndCreateAnswerExplicitly(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + for _, typ := range []string{protocol.ChatSessions, protocol.ChatCreate} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: typ, ID: "c1"}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || response.ID != "c1" { + t.Fatalf("%s did not answer with an explicit error: %+v", typ, response) + } + } +} diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 7d071be3..e237010a 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "strings" + "sync" "time" "github.com/coder/websocket" @@ -21,6 +22,10 @@ type Options struct { // ArtifactsRoot overrides where stage artifacts live. Empty uses the store's // directory (the database's parent), matching the data-directory default. ArtifactsRoot string + // Chat brokers OpenCode chat traffic over /connect. Nil answers every + // chat_* request with a protocol error; a configured server passes an + // *opencode.Chat bound to the owned endpoint. + Chat chatBroker } // Handler serves the persistent connection with default options. Canceling ctx @@ -34,6 +39,7 @@ func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler { func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipeline.Store) http.Handler { build := buildinfo.Current() started := time.Now() + hub := newChatHub() mux := http.NewServeMux() var runs *runner var runErr error @@ -70,16 +76,27 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin } defer conn.CloseNow() conn.SetReadLimit(2 << 20) + // connCtx is canceled when this connection's read loop returns, so a + // chat subscription started for the client stops on disconnect. A single + // write mutex serializes the read loop's responses and the chat stream + // goroutine, which must never interleave partial websocket writes. + connCtx, connCancel := context.WithCancel(ctx) + defer connCancel() + var writeMu sync.Mutex + write := func(message protocol.Message) error { + writeMu.Lock() + defer writeMu.Unlock() + writeCtx, cancel := context.WithTimeout(connCtx, 5*time.Second) + defer cancel() + return wsjson.Write(writeCtx, conn, message) + } if len(stores) > 0 { items, err := stores[0].List(ctx) if err != nil { log.Printf("list pipelines: %v", err) return } - writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second) - err = wsjson.Write(writeCtx, conn, protocol.Message{Type: protocol.PipelineList, Pipelines: items, NotificationEndpoints: notificationEndpoints()}) - cancel() - if err != nil { + if err := write(protocol.Message{Type: protocol.PipelineList, Pipelines: items, NotificationEndpoints: notificationEndpoints()}); err != nil { return } } @@ -89,6 +106,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin return } log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version) + if handleChat(connCtx, request, options.Chat, hub, write) { + continue + } response := protocol.Message{Type: protocol.Pong, ID: request.ID} if request.Type == protocol.List || request.Type == protocol.Submit { response.NotificationEndpoints = notificationEndpoints() @@ -196,10 +216,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin response.Error = "execution unavailable: " + runErr.Error() } } - writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second) - err := wsjson.Write(writeCtx, conn, response) - cancel() - if err != nil { + if err := write(response); err != nil { log.Printf("write response: %v", err) return }